[wp-hackers] checking if post has shortcode, before header is output?

Aaron D. Campbell aaron at xavisys.com
Sun Oct 25 01:02:26 UTC 2009


That won't work for any custom queries (as well as a few other edge 
cases like [[your_shortcode), but it'll work for most scenarios.  You 
could also have your shortcode handler add an action that hooks into 
wp_footer and uses wp_print_scripts() to load your js file.  You get the 
benefit of the js loading in the footer as well as the functionality you 
wanted of only loading your js when needed.


scribu wrote:
> Try this:
>
> add_action('template_redirect', 'add_scripts');
>
> function add_scripts() {
> if ( ! is_page() )
> return;
>
> global $posts;
>
> if ( FALSE === strpos($posts[0]->post_content, '[your_shortcode') )
> return;
>
> wp_enqueue_script(...
> }
>
>
>   



More information about the wp-hackers mailing list