Title: Optimize script &amp; style load only when needed
Last modified: December 29, 2018

---

# Optimize script & style load only when needed

 *  [SharmPRO](https://wordpress.org/support/users/sharmpro/)
 * (@sharmpro)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/optimize-script-style-load-only-when-needed/)
 * Hi, did find really helpful to optimize and easy to optimize script & style load
   this way.
    It seems odd not to have an option on the plugin itself to do it…
 *     ```
       add_action('wp_print_scripts', 'my_remove_cf7_js');
       function my_remove_cf7_js() {
       if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
               global $post;
               if(!has_shortcode($post->post_content,'cf7-form') && !!has_shortcode($post->post_content,'contact-form-7') ){
       	      wp_deregister_script('contact-form-7');
          		}	
       	}
       }
       add_action('wp_print_styles', 'my_remove_cf7_css');
       function my_remove_cf7_css() {
       	if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
       		global $post;
               if(!has_shortcode($post->post_content,'cf7-form') && !!has_shortcode($post->post_content,'contact-form-7')){
                   wp_deregister_style('contact-form-7');
          		}	
       	}
       }
       ```
   

The topic ‘Optimize script & style load only when needed’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-7/assets/icon.svg?rev=2339255)
 * [Contact Form 7](https://wordpress.org/plugins/contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7/reviews/)

## Tags

 * [enhancement](https://wordpress.org/support/topic-tag/enhancement/)
 * [load](https://wordpress.org/support/topic-tag/load/)
 * [performance](https://wordpress.org/support/topic-tag/performance/)

 * 0 replies
 * 1 participant
 * Last reply from: [SharmPRO](https://wordpress.org/support/users/sharmpro/)
 * Last activity: [7 years, 5 months ago](https://wordpress.org/support/topic/optimize-script-style-load-only-when-needed/)
 * Status: not a support question