Title: Loading JavaScript and Stylesheet Only When it is Necessary
Last modified: August 19, 2016

---

# Loading JavaScript and Stylesheet Only When it is Necessary

 *  [Sina Saeedi](https://wordpress.org/support/users/melodymag/)
 * (@melodymag)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/loading-javascript-and-stylesheet-only-when-it-is-necessary/)
 * I read [this](http://contactform7.com/2009/11/22/loading-javascript-and-stylesheet-only-when-it-is-necessary/)
   and it was very helpfull for speeding up loading my website.
 * Is there a way to do the same with “wp-e-commerce” and “nextgen-gallery” and “
   simplemodal-login” ?

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [acenik10](https://wordpress.org/support/users/acenik10/)
 * (@acenik10)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/loading-javascript-and-stylesheet-only-when-it-is-necessary/#post-1918845)
 * yes there is add this to your functions file should work 🙂
 * i check for the pages im loading in this condition only if its my front page 
   i load the scripts n style, you can put your condition
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_
 *  [brasofilo](https://wordpress.org/support/users/brasofilo/)
 * (@brasofilo)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/loading-javascript-and-stylesheet-only-when-it-is-necessary/#post-1918946)
 * yes, the tip Sina links to is very helpful
 * and i found an optimization
    there’s another tip [here](http://www.clickonf5.org/wordpress/optimize-contact-form7-wordpress-plugin/4576/)
   but it does edit the core files
 * but, if we put the two lines in config.php
 *     ```
       define(‘WPCF7_LOAD_JS’, false);
       define(‘WPCF7_LOAD_CSS’, false);
       ```
   
 * and then before wp_head() we put:
 *     ```
       if ( $_SERVER['REQUEST_URI'] == "/"
       or   $_SERVER['REQUEST_URI'] == "/contact/") {
          if ( function_exists( ‘wpcf7_enqueue_scripts’ ) ) {
             wpcf7_enqueue_scripts();
             wpcf7_enqueue_styles();
          }
       }
       ```
   
 * this way, there’s no need to edit core files, or use custom pages, and depending
   on the theme, even custom headers…
 * the REQUEST_URI are the pages/posts with ContactForm7
    and to check them out 
   you can do: `echo "the url = " . $_SERVER['REQUEST_URI'];`
 *  [fotofashion](https://wordpress.org/support/users/fotofashion/)
 * (@fotofashion)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/loading-javascript-and-stylesheet-only-when-it-is-necessary/#post-1918968)
 * @ brasofilo
 * Where exactly do I have to put this? You said “before wp_head()”. Does that mean
   before the wp_head() call in my theme’s header.php? I tried it, but couldn’t 
   get it to work.
 * If I have only one page that uses the contact form, would this be correct?
 *     ```
       <?php
       	if ( $_SERVER['REQUEST_URI'] == "/kontakt/") {
          		if ( function_exists( ‘wpcf7_enqueue_scripts’ ) ) {
             			wpcf7_enqueue_scripts();
             			wpcf7_enqueue_styles();
          }
       }
       ?>
       ```
   
 *  [brasofilo](https://wordpress.org/support/users/brasofilo/)
 * (@brasofilo)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/loading-javascript-and-stylesheet-only-when-it-is-necessary/#post-1918969)
 * [@fotofashion](https://wordpress.org/support/users/fotofashion/)
 * yes, in header.php
 * try this, just for checking the if’s are being accepted
 *     ```
       <?php
       	if ( $_SERVER['REQUEST_URI'] == "/kontakt/") {
       		echo "this is kontakt page<br />";
          		if ( function_exists( ‘wpcf7_enqueue_scripts’ ) ) {
       			echo "function exists, enqueuing scripts<br />";
             			wpcf7_enqueue_scripts();
             			wpcf7_enqueue_styles();
          }
       }
       ?>
       ```
   
 * you’ll have to check the html source to see the echo’s
 *  [fotofashion](https://wordpress.org/support/users/fotofashion/)
 * (@fotofashion)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/loading-javascript-and-stylesheet-only-when-it-is-necessary/#post-1918972)
 * @ brasofilo
 * Ah, works like a charm now. Thanks a lot! The echo’s helped to locate the problem.
   I had to replace the backticks with straight ticks, like this:
 *     ```
       <?php
       	if ( $_SERVER['REQUEST_URI'] == "/kontakt/") {
          		if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
             			wpcf7_enqueue_scripts();
             			wpcf7_enqueue_styles();
          }
       }
       ?>
       ```
   
 *  [brasofilo](https://wordpress.org/support/users/brasofilo/)
 * (@brasofilo)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/loading-javascript-and-stylesheet-only-when-it-is-necessary/#post-1918973)
 * i hate those microscopic errors
    🙂
 *  [marky_uk](https://wordpress.org/support/users/marky_uk/)
 * (@marky_uk)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/loading-javascript-and-stylesheet-only-when-it-is-necessary/#post-1918983)
 * Not sure if it helps anyone, but instead of a particular page URL, I’m allowing
   the JS/CSS to be loaded based on a CUSTOM VARIABLE in the admin front-end – so
   no coding changes as my site scales ….
 * In my config.php I have the two lines:
 *     ```
       define('WPCF7_LOAD_JS', false);
       define('WPCF7_LOAD_CSS', false);
       ```
   
 * In my page.php (or suitable PHP files you need in your theme), first line is:
 * `<?php if (get_post_meta($wp_query->post->ID, 'wpcf7',true)) { if ( function_exists('
   wpcf7_enqueue_scripts' ) ) { wpcf7_enqueue_scripts(); wpcf7_enqueue_styles();}}?
   >`
 * I actually use this technique for quite a few plugins that load across an entire
   site but you want per-page loading.
 * Hope that helps someone out there!
 *  [brasofilo](https://wordpress.org/support/users/brasofilo/)
 * (@brasofilo)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/loading-javascript-and-stylesheet-only-when-it-is-necessary/#post-1919016)
 * [@marky_uk](https://wordpress.org/support/users/marky_uk/)
 * cool, an elegant solution
 * thx

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Loading JavaScript and Stylesheet Only When it is Necessary’ is closed
to new replies.

 * 8 replies
 * 5 participants
 * Last reply from: [brasofilo](https://wordpress.org/support/users/brasofilo/)
 * Last activity: [14 years, 11 months ago](https://wordpress.org/support/topic/loading-javascript-and-stylesheet-only-when-it-is-necessary/#post-1919016)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
