[wp-hackers] checking if post has shortcode, before header is output?
Chris Scott
cjscott69 at gmail.com
Mon Oct 26 11:57:50 UTC 2009
On Sat, Oct 24, 2009 at 9:02 PM, Aaron D. Campbell <aaron at xavisys.com> 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 at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
More information about the wp-hackers
mailing list