Skip to content

Instantly share code, notes, and snippets.

@aldolat
Created April 13, 2018 20:07
Show Gist options
  • Save aldolat/73964cc1f0b107cd4e428c5f14489b79 to your computer and use it in GitHub Desktop.
Save aldolat/73964cc1f0b107cd4e428c5f14489b79 to your computer and use it in GitHub Desktop.
Loads Javascript and CSS of Contact Form 7 plugin only when necessary.
<?php
/**
* Removes Javascript and styles of Contact Form 7 plugin.
* Put this function in your theme's functions.php file.
*
* @see https://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/
*/
function my_theme_setup() {
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
}
add_action( 'after_setup_theme', 'my_theme_setup', 11 );
/**
* Enqueue Javascript and styles of Contact Form 7 plugin only when necessary.
* Change $my_contact_page as required.
* Put this function in your theme's functions.php file.
*
* @see https://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/
*/
function aldolat_scripts() {
$my_contact_page = array(
'Contact',
'My Page',
'My Other Page',
// add here other pages...
);
if ( is_page( $my_contact_page ) ) {
if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
wpcf7_enqueue_scripts();
}
if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
wpcf7_enqueue_styles();
}
}
}
add_action( 'wp_enqueue_scripts', 'aldolat_scripts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment