Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active March 12, 2019 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsword/168d8d94e47844c2a9d6f783f60b38b9 to your computer and use it in GitHub Desktop.
Save davidsword/168d8d94e47844c2a9d6f783f60b38b9 to your computer and use it in GitHub Desktop.
WP - pass variable into wp_footer and other actions hooks and filters
<?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