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

Joost de Valk joost at yoast.com
Wed Apr 22 17:40:11 GMT 2009


Wouldn't that break XHTML validity?

On 22 apr 2009, at 19:18, "Aaron D. Campbell" <aaron at xavisys.com> 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:
>>
>> 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.
> _______________________________________________
> 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