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. :-)
wp-hackers-boun...@lists.automattic.com a écrit sur 22/04/2009 13:38:04 :
> 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.
If you want to load the style only on some pages, maybe you can add it inline? If so, testing the presence of the shortcode in each post is easy (and you can also make it so it loads once in case multiple posts are displayed)
Thanks for the reply Ozh - inline styling could work but would be really ugly, as my plugin involves a fairly heavily styled form that uses an ordered list for organizing the form elements. It would also mean users would have to hack my code if they wanted to customize the display (I suppose I could put in placeholder class declarations too, but then we're getting into a workaround on top of a workaround). Mike T Date: Wed, 22 Apr 2009 13:50:00 +0200 From: "Ozh" <o...@planetozh.com> Subject: RE [wp-hackers] loading css only on page where plugin is used To: wp-hack...@lists.automattic.com Message-ID: <OF6660D081.F3C53FEC-ONC12575A0.0040D66D-C12575A0.00410...@notes.edfgdf.fr> Content-Type: text/plain; charset="ISO-8859-1" wp-hackers-boun...@lists.automattic.com a écrit sur 22/04/2009 13:38:04 :
> > 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.
If you want to load the style only on some pages, maybe you can add it inline? If so, testing the presence of the shortcode in each post is easy (and you can also make it so it loads once in case multiple posts are displayed)
That will only work with the default queries, if someone does a custom query it won't catch it. I'd recommend the inline idea and just set a flag so the styles are only added once.
> That will only work with the default queries, if someone does a > custom query it won't catch it. I'd recommend the inline idea and > just set a flag so the styles are only added once.
> aesqe wrote: >> Michael, try using something like this:
>> On 22.4.2009 13:38, Michael Toppa wrote: >>> 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. > _______________________________________________ > wp-hackers mailing list > wp-hack...@lists.automattic.com > http://lists.automattic.com/mailman/listinfo/wp-hackers
> That will only work with the default queries, if someone does a custom > query it won't catch it. I'd recommend the inline idea and just set a > flag so the styles are only added once.
> aesqe wrote: >> Michael, try using something like this:
>> On 22.4.2009 13:38, Michael Toppa wrote: >>> 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. > _______________________________________________ > wp-hackers mailing list > wp-hack...@lists.automattic.com > http://lists.automattic.com/mailman/listinfo/wp-hackers
} aesqe wrote: > sorry for asking what might seem like a pretty dumb question, but what > kind of custom query? could you give an example? :/
> On 22.4.2009 19:18, Aaron D. Campbell wrote: >> That will only work with the default queries, if someone does a custom >> query it won't catch it. I'd recommend the inline idea and just set a >> flag so the styles are only added once.
>> aesqe wrote: >>> Michael, try using something like this: