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.

1 Answer 1


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(); ?>
  • oh, so you are parsing the actual page content for that ?
    – SpyrosP
    Jul 11, 2011 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. Jul 11, 2011 at 21:34
  • ok i see thx :)
    – SpyrosP
    Jul 11, 2011 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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