Last active
March 12, 2019 18:07
-
-
Save davidsword/168d8d94e47844c2a9d6f783f60b38b9 to your computer and use it in GitHub Desktop.
WP - pass variable into wp_footer and other actions hooks and filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Pass a value to a hook in an annonymous function | |
add_shortcode('show_weather',function(){ | |
$a = shortcode_atts( array( | |
'location' => 'Vancouver, BC', | |
), $atts, 'show_weather' ); | |
$city = $a['location']; | |
// somthing for the placeholder | |
$return = "<div id='weather'><span>{$city}</span></div>"; | |
// resources required, but in footer | |
// that require shortcode attribute `location` | |
add_action('wp_footer', function() use ($city) { // 🎉 | |
//echo "<script> ... location: {$city}; ... </script>"; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment