I'm using the following function in order to only enqueue Contact Form 7 plugin dependencies if its shortcode is present within the page content.
If I place CF7 shortcode directly in the editor while creating a page, the bellow function works, however, I'm building page templates with AQPB and so CF7 shortcode is present within my AQPB blocks, what forces me to place only the AQPB shortcode in the editor.
How would one check for the presence of a shortcode within the page content coming from the AQPB templates?
/**
* Contact Form 7 - Load css, js only when its necessary
* Enqueue Contact Form 7 plugin dependencies only if its shortcode is present
*/
function cf7_dequeue_scripts() {
$load_scripts = false;
if( is_front_page() | is_page() | 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', 'cf7_dequeue_scripts', 99 );
I'm using the following function in order to only enqueue Contact Form 7 plugin dependencies if its shortcode is present within the page content.
If I place CF7 shortcode directly in the editor while creating a page, the bellow function works, however, I'm building page templates with AQPB and so CF7 shortcode is present within my AQPB blocks, what forces me to place only the AQPB shortcode in the editor.
How would one check for the presence of a shortcode within the page content coming from the AQPB templates?