Created
July 15, 2017 04:36
-
-
Save cyberwani/38f945007e83600ca0bf0bc48d99361b to your computer and use it in GitHub Desktop.
Contact Form 7 plugin: Load Javascript and CSS on Pages, Posts and CPTs where Contact Form 7 shortcode is used.
This file contains 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 | |
// Unload CF7 assests | |
add_filter( 'wpcf7_load_js', '__return_false' ); | |
add_filter( 'wpcf7_load_css', '__return_false' ); | |
add_action( 'the_content', 'load_cf7_assets' ); | |
function load_cf7_assets($content){ | |
global $post; | |
// Load assests when content contains CF7 shortcode. | |
$post_content = $post->post_content; | |
if ( has_shortcode( $post_content, 'contact-form-7' ) ) { // Check if content contains CF7 shortcode | |
// Load CF7 Javascript | |
if ( function_exists( 'wpcf7_enqueue_scripts' ) ) { | |
wpcf7_enqueue_scripts(); | |
} | |
// Load CF7 CSS | |
if ( function_exists( 'wpcf7_enqueue_styles' ) ) { | |
wpcf7_enqueue_styles(); | |
} | |
} | |
return $content; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment