certainlyakey
6/2/2015 - 6:45 PM

Wordpress - load WP Contact Form 7 plugin dependencies only if its shortcode is present

Wordpress - load WP Contact Form 7 plugin dependencies only if its shortcode is present

//Load WP Contact Form 7 plugin dependencies only if its shortcode is present
function dvk_dequeue_scripts() {
	$load_scripts = false;
	if( is_singular() ) {
		$post = get_post();
		if( has_shortcode($post->post_content, 'contact-form-7') ) {
			$load_scripts = true;
			//Additional scripts that may be used with CF7
			// wp_enqueue_style( 'select2-style', '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css' );
			// wp_enqueue_script( 'select2-js', '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js' );
		}
	}
	if( ! $load_scripts ) {
		wp_dequeue_script( 'contact-form-7' );
		wp_dequeue_style( 'contact-form-7' );
	}
}

add_action( 'wp_enqueue_scripts', 'dvk_dequeue_scripts', 99 );