• Hi!

    By default, the plugin loads its own JS & CSS files on every page, but in most cases this is not necessary, since we mostly have a contact page with a form. With the help of the small piece of code below, we can automatically limit the loading of these files to only the page/pages where the form is displayed. In this way, we can speed up the loading speed of our website and our code remains a little cleaner.:
    function dvk_dequeue_scripts() {
    $load_scripts = false;
    if( is_singular() ) {
    $post = get_post();
    if( has_shortcode($post->post_content, ‘contact-form-7’) ) {
    $load_scripts = true;
    }
    }
    if( ! $load_scripts ) {
    wp_dequeue_script( ‘contact-form-7’ );
    wp_dequeue_style( ‘contact-form-7’ );
    }
    }
    add_action( ‘wp_enqueue_scripts’, ‘dvk_dequeue_scripts’, 99 );

    Thanks!

The topic ‘Only load Contact Form 7 scripts when needed’ is closed to new replies.