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

Aaron D. Campbell aaron at xavisys.com
Wed Apr 22 17:18:22 GMT 2009


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:
>
> function shortcode_css()
> {
>   global $wp_query;
>     
>   if( !empty($wp_query->posts) )
>   {
>     foreach( $wp_query->posts as $post )
>     {
>       if(preg_match("#\[your_shortcode[^\]]*\]#is", $post->post_content)
>         wp_enqueue_style( "your css name", "your css location" );
>     }
>   }
> }
> add_action('wp_print_styles', 'shortcode_css');
>
> 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.


More information about the wp-hackers mailing list