Last active
May 11, 2021 11:53
-
-
Save FutureMedia/b798e2515f7856a59f6caacd046ea84c to your computer and use it in GitHub Desktop.
Contact Form 7 (CF7) - load JS & CSS only when it is necessary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* CF 7 - version 5.4.1 | |
* Load JS & CSS only when necessary | |
* | |
* Use WP functions '__return_true', '__return_false' to load the | |
* necessary files. The 'is_page()' should be called after the init | |
* hook and the js+css must be enqueued before the wp_head hook. | |
* | |
*/ | |
add_filter( 'wpcf7_load_js', '__return_false' ); | |
add_filter( 'wpcf7_load_css', '__return_false' ); | |
function fm_cf7_activate_scripts() { | |
if (is_page('contact') || is_page('45') || is_singular( array( 'cd', 'book' ) )) { | |
add_filter( 'wpcf7_load_js', '__return_true' ); | |
add_filter( 'wpcf7_load_css', '__return_true' ); | |
} | |
} | |
add_action( 'get_header', 'fm_cf7_activate_scripts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment