[wp-hackers] Selective CSS based on Shortcode?

Otto otto at ottodestruct.com
Wed Jan 27 20:04:50 UTC 2010


Also consider using post_class(); and the post_class filter instead,
to apply a specific class to the post when it contains a shortcode.

Something like this:

add_filter('post_class','add_my_class',10,3);
function add_my_class($classes, $class, $postid) {
  $post = get_post($postid);
  if (false !== stripos($post->post_content, '[code]') {
    $classes[] = 'my-class';
  }
  return $classes;
}

That will add "my-class" to the post_class output whenever the post
contains [code], for example. using that, you can rather easily use
.my-class to add styling for those specific posts containing a
shortcode. Simple, fast, no need for look-ahead in the header...

-Otto
Sent from Memphis, TN, United States


On Wed, Jan 27, 2010 at 1:58 PM, Otto <otto at ottodestruct.com> wrote:
> Sounds to me like you're going about it the wrong way in the first
> place. The whole idea behind CSS is that it is overridable to begin
> with. You shouldn't need to detect the shortcode and include extra CSS
> based on it.
>
> Why can't you make your plugin create an appropriate class surrounding
> its output, then make your CSS act accordingly?
>
> -Otto
> Sent from Memphis, TN, United States
>
>
> On Wed, Jan 27, 2010 at 2: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?
>>
>> Thanks
>> Matt
>>
>> --
>> Matthew Kettlewell
>>
>> http://www.TazaTek.com
>> http://www.twitter.com/kettlewell
>> _______________________________________________
>> 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