1

I'm trying to add CSS to wp_head, from within the method called to render a shortcode. I know that wp_head does not take any parameters, so I'm trying to figure out the following:

The method called by WP to render the shortcode has two params, $atts and $content. What I'm trying to do is generate instance specific CSS from the $atts provided, and add it to the header, rather than rendering a tag in my method, I'd like all of my CSS in one spot (I'm adding a numerical ID to each rule, which allows custom CSS even if its the same shortcode).

The troubles I'm having:

  1. I can't just pass a string to wp_head using add_actionm it requires a function
  2. Because I need the $atts provided by wordpress, the function has to accept a parameter, which php 5.2 doesn't support (minimum supported php version for WP).
  3. WP doesnt have a way to called something like.. content_for in rails, so I cant just append it and put it where I want.

I've tried filters, actions, *wp_add_inline_styles*, and even globals with no success. Any help would be greatly appreciated.

2
  • 3
    The wp_head action is fired before the shortcode is generated via the do_shortcode callback of the the_content filter. Some solutions try to peek into the post_content earlier on with regex (for example via the the_posts filter) to check if it contains any shortcodes. You should try to search this site to find these "peek" solutions.
    – birgire
    Dec 16, 2013 at 21:49
  • 2
    In essence, you need this: wordpress.stackexchange.com/a/101515/21376
    – s_ha_dum
    Dec 17, 2013 at 0:43

0

Browse other questions tagged or ask your own question.