6

I have plugin I would like to use only on the contact page, however the script and css is being loaded on every page. Aside from the increased http requests, it is also causing another script to not work properly on another page.

Is there a way to stop this plugin from loading anywhere else on the site?

I know how to do this with normal scripts in the header, but I am not sure how to do this with a plugin that automatically injects scripts into the header.

Thanks in advance.

3 Answers 3

4

I have figured it out.

There is a good explanation here

You edit the wp.config file with the scripts you wish to not load...

define('WPCF7_LOAD_JS', false);

...and then manually enqueue them on the pages you wish to load the script.

1
  • To finish out the answer (as indicated in the link above), do this as well: 1) To stop the CSS from running everywhere, also add this line to your wp-config.php: define('WPCF7_LOAD_JS', false); 2) To enqueue CSS and JS: Edit the contact.php template file, inserting: <?php if ( function_exists( 'wpcf7_enqueue_scripts' ) ) { wpcf7_enqueue_scripts(); } if ( function_exists( 'wpcf7_enqueue_styles' ) ) { wpcf7_enqueue_styles(); } ?> Note:wpcf7_enqueue_scripts() and wpcf7_enqueue_styles() must be called before wp_head() is called. Commented May 24, 2019 at 16:14
2

Try using

if(is_page('contact-page-slug')){ 

to add your functions to that page only.

2

Try this CODE:

if ( ! is_page('THE NAME OF THE PAGE WHERE YOU WANT CF7 TO RUN') ) {
    add_filter( 'wpcf7_load_js', '__return_false' );
    add_filter( 'wpcf7_load_css', '__return_false' );
}

Source: From the official CF7 page.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.