Skip to content

Instantly share code, notes, and snippets.

@gzalinski
Last active May 25, 2022 13:59
Show Gist options
  • Save gzalinski/7381a41afa73352b068717b7e2acfae8 to your computer and use it in GitHub Desktop.
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
/**
* 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