Skip to content

Instantly share code, notes, and snippets.

@SHABU89
Created October 20, 2018 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SHABU89/7afbe5b337cd5c4ca58f07b185c69fbc to your computer and use it in GitHub Desktop.
Save SHABU89/7afbe5b337cd5c4ca58f07b185c69fbc to your computer and use it in GitHub Desktop.
Contact Form 7 Scripts and Stylesheet files only add if shortcode present in the page else remove it.
function rjs_lwp_contactform_css_js() {
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'contact-form-7') ) {
wp_enqueue_script('contact-form-7');
wp_enqueue_style('contact-form-7');
}else{
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'rjs_lwp_contactform_css_js');
@vicentor
Copy link

vicentor commented Jan 8, 2021

This is nice, but what if the shortcode is contained in a widget or a related post? (some themes put the header/footer in a different post type that is linked to the page post)

@SHABU89
Copy link
Author

SHABU89 commented Jan 9, 2021

This is nice, but what if the shortcode is contained in a widget or a related post? (some themes put the header/footer in a different post type that is linked to the page post)

Thanks for your comment. This won't work in your case if the shortcode present in the footer or widget the above function is only for the post content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment