Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.