Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cyberwani/38f945007e83600ca0bf0bc48d99361b to your computer and use it in GitHub Desktop.
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.
<?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