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

Aaron D. Campbell aaron at xavisys.com
Mon Oct 26 17:41:37 UTC 2009


That still doesn't fix the case where more than one query is ran on a 
page, when someone does something like "$myQuery = new 
WP_Query($params);" you'd process the main query, but you wouldn't get 
any of the shortcodes in the custom query.

Chris Scott wrote:
> 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
> }
>   



More information about the wp-hackers mailing list