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

William Canino william.canino at googlemail.com
Mon Oct 26 19:05:38 UTC 2009

Basically you have to predict at wp_head which posts will have their
the_content displayed on the page.

As others said, at the time of wp_head you cannot know what part in
the page will display content that may contain that shortcode.  The
'template_redirect' suggestion doesn't cover archive pages and the
blog front page. To really cover all bases, you will need to generate
the whole page unto a buffer first to find out, and if it does, insert
your script and css.

Another inelegant solution is to store all the possible REQUEST_URIs
that will have the shortcode into a table, and at wp_head use this
table to decide.

Or lastly, just give this up, and insert your CSS (invalid) and JS
(okay) in the footer. Strictly speaking, stylesheets and <style> tags
must be in the <head> tag.

As I said, I don't have the answer for this.

2009/10/23 Kevin Newman <CaptainN at unfocus.com>:
> Hello,
> Is there anyway to check if there your registered shortcode (for a plugin)
> is being used in the current post, during some action hook before the header
> is output? The idea would be to include CSS and JS files for the shortcode,
> only on pages that actually use the shortcode. I'm doing it inline right
> now, since wp_enqueue_script is not available where the shortcode is dealt
> with.
> Thanks,
> Kevin N.
> _______________________________________________
> 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