Support » Plugin: Contact Form 7 » Only load scripts and style when page have a form

  • Creame

    (@creapuntome)


    I write a simple code to enqueue assets only on pages that have a form. Simply add it to your theme functions.php:

    // Disable contact-form-7 enqueue actions
    remove_action( 'wp_enqueue_scripts', 'wpcf7_do_enqueue_scripts', 10 );
    remove_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 10 );
    
    // Trigger contact-form-7 enqueue actions when form shortcode is called
    function contact_form_7_enqueue_scripts($out){
    	wpcf7_do_enqueue_scripts();
     	wpcf7_recaptcha_enqueue_scripts();
    	return $out;
    }
    add_filter( 'shortcode_atts_wpcf7', 'contact_form_7_enqueue_scripts' );

    You can also comment and collaborate on this gist

  • You must be logged in to reply to this topic.