Enable Contact form 7 on specified pages only otherwise wp_deregister_script

Optimizer Review - widget areas

Like many plugins Contact form 7 adds CSS and Javascript to the head of your HTML document. However like many plugins this is done on every single page regardless if the code is required. Adding this snippet to the functions.php of your wordpress theme will let you enable Contact form 7 only on specified pages.

You will see !is_page(array(8,10)) within each array, the 8 and 10 are the IDs of the posts you wish to add the CSS and Javascript to head of your HTML document.

add_action( 'wp_print_scripts', 'deregister_cf7_javascript', 100 );
function deregister_cf7_javascript() {
    if ( !is_page(array(8,10)) ) {
        wp_deregister_script( 'contact-form-7' );
add_action( 'wp_print_styles', 'deregister_cf7_styles', 100 );
function deregister_cf7_styles() {
    if ( !is_page(array(8,10)) ) {
        wp_deregister_style( 'contact-form-7' );

Comments  Leave a Reply

  1. Don’t forget to issue wp_dequeue_style( ‘contact-form-7’ ) and wp_dequeue_script( ‘contact-form-7’ ) after wp_deregister_style( ‘contact-form-7’ ) and wp_deregister_script( ‘contact-form-7’ ) respectively!

  2. For a few years we were using this plugin on clients sites and this was very annoying. In fact it’s the main reason we stopped using it. Anyway, glad there’s now a ‘fix’ for this issue, I’m sure many people will be thankful for it

    1. No problem glad to help,

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!