[wp-hackers] Selective CSS based on Shortcode?

John R R Leavitt jrrl at jrrl.com
Wed Jan 27 15:20:05 UTC 2010


Another method of handling this would be to make sure you leave a marker 
of some sort in the HTML generated by your shortcode and include some 
javascript to selectively include the CSS if necessary.  Of course, you 
need to include your script entirely in the header or you are just 
trading a script fetch for a CSS fetch (and making two fetches if you do 
need the CSS).  So something like this (assuming jQuery is already being 
used):

<script type="text/javascript">
$(document).ready(function() {
    if ($('.YOURCLASS').length) {
        $('head').append('<link rel="stylesheet" href="YOURSTYLESHEET" 
type="text/css" />');
    });
});
</script>

Of course, what we really need is one last filter once the entirely page 
is generated, but that would delay sending the page to the user, which 
would stink.

Andrew Nacin wrote:
> I would read through this wp-hackers thread:
>
> http://lists.automattic.com/pipermail/wp-hackers/2009-October/028143.html
> http://lists.automattic.com/pipermail/wp-hackers/2009-October/thread.html#28143
>
> On Wed, Jan 27, 2010 at 3:32 AM, Matthew Kettlewell <ceo at kettlewell.net>wrote:
>
>   
>> I'm wanting to override CSS in posts that use a shortcode from my
>> plugin....
>>
>> problem is that I'm not sure how to go about doing this...
>>
>> I'm using add_shortcode() to check in the post contents for the shortcode,
>> but I'm needing to know if the shortcode is used when I'in in header.php.
>>
>> I've tried setting meta_data in my function that is called from
>> add_shortcode(), but it's too late...
>>
>> Is there a way to get the shortcode value before header.php is called?
>>
>>     


More information about the wp-hackers mailing list