• Resolved Andry

    (@blackstar1991)


    Can someone told what I need to write in functions.php that scripts and styles contact-form-7 include only in one page. Contact page for example.

    I tried deregistered scripts and styles of plugin.

    add_action('wp_footer', function (){
        wp_deregister_style('contact-form-7-css');
        wp_deregister_script('contact-form-7-js-extra');
        wp_deregister_script('contact-form-7-js');
    
        if (is_page('contact')):
            wp_enqueue_style('contact-form-7-css', get_template_directory_uri() . '/wp-content/plugins/contact-form-7/includes/css/styles.css', '', null, false);
            wp_enqueue_script('contact-form-7-js', get_template_directory_uri() . '/wp-content/plugins/contact-form-7/includes/js/index.js', '', null, true);
        endif;
    
    });

    But this is not the right approach.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi, there’s a support doc on the Contact Form 7 website which I think should help:

    https://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/

    Thread Starter Andry

    (@blackstar1991)

    Thanks for answer. It’s mostly decide my problem.

    When I added script into header part of my content page

    <?php
    get_header();
    if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
      wpcf7_enqueue_scripts();
    } 
    if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
      wpcf7_enqueue_styles();
    }

    I got including styles contact-form-7-css into footer part.

    How I can come back contact-form-7-css into <head> tag of this page ?

    • This reply was modified 1 year, 10 months ago by Andry.

    Hi, I’m not sure how you’d control where the CSS is loaded I’m afraid. I think wpcf7_enqueue_styles() just calls the standard wp_enqueue_style() to load a CSS file which doesn’t use any method of targeting header or footer, and I think this should just load in the header by default.

    However, I see in your code you have get_header() just above the two if blocks, so I think this may be your problem as your header.php file probably has the wp_head() function in it, this function loads in any CSS and JS but as your two if statements are after get_header() I don’t think they’re getting called at the right time. Try putting the two if statements before get_header() is called and see if that makes any difference.

    One other tweak would be to put the two if statements in your functions.php file instead and use is_page(), e.g:

    if ( is_page( 'contact ) ) { // use the slug of the page your form is on
    if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
    wpcf7_enqueue_scripts();
    }
    if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
    wpcf7_enqueue_styles();
    }
    }

    I would check first by putting the if blocks before get_header() and see if that fixes the loading issue.

    Thread Starter Andry

    (@blackstar1991)

    Yes, thanks. When I put if ( function_exists( 'wpcf7_enqueue_styles' ) ) { wpcf7_enqueue_styles(); } before get_header(); code worked correctly

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How does Include plugin scripts and styles only for one page?’ is closed to new replies.