• Hello.
    If I activate reCaptcha with Contact From 7 the captcha code is loaded on each page, regardless of whether it is a page with a form or not.
    I’m wondering if and how it is possible to load the reCaptcha script only on the pages with a contact form.
    Can somebody point me to the right direction, if there is a hook or a configuration part which helps me to achieve this goal?!
    Thank you in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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

    As a follow-up, I’m not sure if the CF7 developers read these things, but it would be great if the reCAPTCHA module honored the WPCF7_LOAD_JS constant. Then one could simply follow the existing instructions for conditional loading available here:

    https://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Load reCaptcha only on CF7 pages’ is closed to new replies.