WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i'm creating some custom shortcodes for my wordpress theme. Shortcodes are by nature optional on pages/posts, but may contain css, js or other files.

My question is, how can i enqueue css styles ONLY IF a particular shortcode has been used ? I would not want to load everything for no reason.

share|improve this question
up vote 1 down vote accepted

The general process for this is:

  1. Create a function, hooked into wp_enqueue_scripts
  2. Inside your function, cycle through the Loop, looking for your shortcode string
  3. If found, call wp_enqueue_style( 'my_custom_style' )
  4. Rewind the Loop, by calling <?php rewind_posts(); ?>
share|improve this answer
oh, so you are parsing the actual page content for that ? – SpyrosP Jul 11 '11 at 20:14
Well, you're cycling through the Loop, but not actually outputting any of the Loop content. You're just looking for a preg_match() or similar for your shortcode string. – Chip Bennett Jul 11 '11 at 21:34
ok i see thx :) – SpyrosP Jul 11 '11 at 23:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.