Last active
May 25, 2022 13:59
-
-
Save gzalinski/7381a41afa73352b068717b7e2acfae8 to your computer and use it in GitHub Desktop.
[Contact Form 7] connect script and style when gutenberg block is detected #cf7 #wp
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
/** | |
* CF7 Optimize Enqueue | |
*/ | |
add_action( 'wp_enqueue_scripts', 'cf7_optimize_equeues' ); | |
add_action( 'admin_enqueue_scripts', 'cf7_optimize_equeues' ); | |
function cf7_optimize_equeues(){ | |
global $post; | |
if( !class_exists( 'WPCF7' ) || !$post ) { | |
return; | |
} | |
wp_dequeue_style('contact-form-7'); | |
wp_dequeue_script('contact-form-7'); | |
if ( has_blocks( $post->post_content ) ) { | |
$blocks = parse_blocks( $post->post_content ); | |
foreach ($blocks as $block){ | |
if( strpos( $block['blockName'], 'contact-form-7') !== false ){ | |
wp_enqueue_style('contact-form-7'); | |
wp_enqueue_script('contact-form-7'); | |
return; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment