• Resolved cooper08

    (@cooper08)


    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!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Bowo

    (@qriouslad)

    @cooper08 this looks useful. However, isn’t this a better suggestion for the developer of Contact Form 7?

    Thread Starter cooper08

    (@cooper08)

    Legitimate. Unfortunately, they haven’t been included in it for years. I wrote to them again.

    Plugin Author Bowo

    (@qriouslad)

    Good luck. I’m marking this resolved for now.

    Thread Starter cooper08

    (@cooper08)

    All right. Unfortunately, Contact Form 7? does not indicate anything to my request.

    • This reply was modified 1 year, 2 months ago by cooper08.
    Plugin Author Bowo

    (@qriouslad)

    That’s quite unfortunate, given how popular the plugin is. Has no one tried to address it even with a code snippet?

    Thread Starter cooper08

    (@cooper08)

    I do not know about it. I usually write it in functions.php.

    Plugin Author Bowo

    (@qriouslad)

    OK. I’ll note this down for now. When I see another popular plugin that needs to be optimized in a similar way, i.e. assets be unloaded when not in use on the front end pages, I’ll consider adding a new “Disable Plugin Assets” module under Disable Components.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Only load Contact Form 7 scripts when needed’ is closed to new replies.