I am accomplishing this with some help from the Advanced Custom Fields plugin. I create a custom Yes/No field for all pages, labeled “Enable Contact Form 7 on this page.”
If you have the ACF plugin installed, simply add the following code to your theme’s functions.php file:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_5d7413a1836ca',
'title' => 'Contact Form 7',
'fields' => array(
array(
'key' => 'field_5d7413a9fdc3f',
'label' => 'Enable Contact Form on this page',
'name' => 'enable_contact_form_on_this_page',
'type' => 'true_false',
'instructions' => 'Set to \'Yes\' to activate CSS & JS for Contact Form 7 & reCAPTCHA v3.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
endif;
Next, I have a bit of code which checks for that custom field and if it’s unset or set to No, I dequeue the reCAPTCHA script and prevent the other CF7 assets from being loaded as well. Add this to your functions.php file:
add_action( 'wp', 'contact_form_7_check' );
function contact_form_7_check() {
if ( get_field( 'enable_contact_form_on_this_page' ) ) {
} else {
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
add_action( 'wp_print_scripts', 'disable_recaptcha' );
}
}
function disable_recaptcha() {
wp_dequeue_script( 'google-recaptcha' );
}
-
This reply was modified 5 years, 6 months ago by
njbair. Reason: removed theme-specific function names