WARNING: This server is unstable and will be retired in the next days. If you want to keep this forum available, please request immediately a migration on the Nabble Support forum. Forums that don't receive any migration request will be deleted forever.

 « Return to Thread: checking if post has shortcode, before header is output?

Re: checking if post has shortcode, before header is output?

by Chris Scott-7 :: Rate this Message:

| View in Thread

On Sat, Oct 24, 2009 at 9:02 PM, Aaron D. Campbell <aaron@...> wrote:
> 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.

To prevent the edge cases you can use get_shortcode_regex() to know
that you are checking for the shortcode the same way wp is. Something
like this:

$pattern = get_shortcode_regex();
preg_match('/'.$pattern.'/s', $posts[0]->post_content, $matches);
if (is_array($matches) && $matches[2] == 'YOURSHORTCODE') {
        //shortcode is being used
}


--
Chris Scott
http://iamzed.com/
http://hailtheale.com/

>
>
> 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(...
>> }
>>
>>
>>
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

 « Return to Thread: checking if post has shortcode, before header is output?