Title: Conditionally Adding Scripts and Styles if Shortcode present
Last modified: August 19, 2016

---

# Conditionally Adding Scripts and Styles if Shortcode present

 *  [Highway of Life](https://wordpress.org/support/users/highway-of-life/)
 * (@highway-of-life)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/conditionally-adding-scripts-and-styles-if-shortcode-present-1/)
 * Hello,
 * I noticed that with many plugins, they include scripts and stylesheets in the
   header even if the shortcode is never used, or not used on the current page. 
   Since this adds to the HTTP requests unnecessarily, I am trying to figure out
   how to conditionally include the scripts and stylesheets only if the shortcode
   is present.
 * The shortcodes are registered with:
    add_shortcode(‘foo’, ‘my_foo’); However,
   it does not appear that I can set a variable in the my_foo() function to tell
   init, enqueue or wp_footer, wp_head, wp_print_* to then include any of the CSS
   or javascripts only if the shortcode is present on the page. I would like to 
   only include the CSS as necessary, and only include the Javascript in the footer,
   not the header.
 * Do any of you have a solution to this?
    Thanks in advance!

Viewing 1 replies (of 1 total)

 *  [ugurcem](https://wordpress.org/support/users/ugurcem/)
 * (@ugurcem)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/conditionally-adding-scripts-and-styles-if-shortcode-present-1/#post-2003575)
 * Check “The Jedi Master way” section at [http://scribu.net/wordpress/optimal-script-loading.html](http://scribu.net/wordpress/optimal-script-loading.html)

Viewing 1 replies (of 1 total)

The topic ‘Conditionally Adding Scripts and Styles if Shortcode present’ is closed
to new replies.

## Tags

 * [enqueue](https://wordpress.org/support/topic-tag/enqueue/)
 * [http requests](https://wordpress.org/support/topic-tag/http-requests/)
 * [performance](https://wordpress.org/support/topic-tag/performance/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [ugurcem](https://wordpress.org/support/users/ugurcem/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/conditionally-adding-scripts-and-styles-if-shortcode-present-1/#post-2003575)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
