Skip to content

Instantly share code, notes, and snippets.

@stevegrunwell
Created June 8, 2012 20:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevegrunwell/2897997 to your computer and use it in GitHub Desktop.
Save stevegrunwell/2897997 to your computer and use it in GitHub Desktop.
Remove Contact Form 7 scripts and styles through the WordPress action hook API
/**
* Remove Contact Form 7's scripts and styles without having to add anything to wp-config.php (as described
* in the CF7 docs) by using the wpcf7_enqueue_styles and wpcf7_enqueue_scripts actions that Takayuki was
* nice enough to include in includes/controller.php
* @link http://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/
*/
add_action( 'wpcf7_enqueue_styles', function() { wp_deregister_style( 'contact-form-7' ); } );
add_action( 'wpcf7_enqueue_scripts', function() { wp_deregister_script( 'jquery-form' ); } );
@alexsoyes
Copy link

Working good ! Thank you. For adopters you might consider using conditions on page.

@garek007
Copy link

garek007 commented Feb 15, 2019

I see this was written 7 years ago, does it still work? I need to deregister scripts then put them in my theme scripts.js file. Odd because @Dinath commented recently that it works, but I don't see a jquery-form.js script anywhere in the plugin folder

@Wendihihi
Copy link

Wendihihi commented Jan 18, 2020

I can confirm that deregistering the styles works, but deregistering the script doesn't work. What does work is this:

Add this to the theme's functions.php file
add_filter( 'wpcf7_load_js', '__return_false' ); add_filter( 'wpcf7_load_css', '__return_false' );

Add this to the theme header.php, just above <?php get_header(); ?>

<?php
if (is_page('the_page_i_want_to_show_the_scripts_and_styles')) { 
    if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
        wpcf7_enqueue_scripts();
    }
  
    if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
        wpcf7_enqueue_styles();
    }
}
?>

Replace 'the_page_i_want_to_show_the_scripts_and_styles' with the page slug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment