[wp-hackers] loading css only on page where plugin is used

Michael Toppa public at toppa.com
Wed Apr 22 11:38:04 GMT 2009

Hi all,

I'm working on a plugin that uses a shortcode. I want to load a css file 
for the plugin only for posts or pages that use the shortcode. I've read 
Vladimir Prelovac's tip related to this at 
http://mashable.com/2009/03/25/wordpress-plugin-developer-tips/ (tip 
number 8) and a few similar posts by others, but they're all talking 
about loading scripts only on appropriate admin pages. My situation is 
not for an admin page.

I tried calling template_redirect (which in turn calls wp_enqueue_style) 
in my plugin after calling add_shortcode and setting a flag indicating 
the presence of my shortcode, but that didn't work (my testing indicates 
Wordpress fires template_redirect before add_shortcode - the order I 
call them in doesn't matter). It may be that it's not possible to 
determine whether to take an action in the head of a page based on an 
action in the body. In that case I can ask my plugin users to provide 
page/post id numbers in my plugin's settings menu, so its css doesn't 
get loaded on every page. But if any of you know a way to enqueue a 
stylesheet based on the presence of a shortcode, please fill me in. :-)


Mike T


More information about the wp-hackers mailing list