Support » Plugin: ReCaptcha v2 for Contact Form 7 » Disable reCaptcha for logged-in users

  • Resolved nlnieuw7

    (@nlnieuw7)


    I’m very happy with your plugin. However, I would like to disable reCaptcha for logged-in users. Any ideas on how to make this work?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author IQComputing

    (@iqcomputing)

    Hello,

    The neat thing about Contact Form 7 is by using both by using shortcodes and utilizing hooks it’s pretty easy to jump in when and where you need to so you can inject your own functionality.

    To remove the reCaptcha for logged in users we first need to override the [recaptcha] shortcode:

    /**
     * Override the reCaptcha shortcode for Contact Form 7 and WPCF7 ReCaptcha
     * Return empty string so it does not display
     *
     * @return void
     */
    function prefix_recaptcha_for_logged_in_users() {
    
    	if( ! is_user_logged_in() ) {
    		return;
    	}
    	
    	wpcf7_remove_form_tag( 'recaptcha' );	// Remove all reCaptcha tags
    	wpcf7_add_form_tag(						// Re-add recaptcha tag
    		'recaptcha',
    		function() {						// Required PHP PHP v5.3+
    			return '';						// Return empty string in shortcode callback
    		},
    		array( 'display-block' => true )
    	);
    
    }
    add_action( 'wpcf7_init', 'prefix_recaptcha_for_logged_in_users', 30 ); // Contact Form 7 uses priority 10, WPCF7 ReCaptcha uses priority 20, so we need to use 30+

    Next we need to remove all reCaptcha related hooks that are added both by WPCF7 ReCaptcha and Contact Form 7. This plugin uses the setup_theme hook so we can add a later priority and remove anything added.

    /**
     * Remove any reCaptcha related hooks added both by Contact Form 7 and WPCF7 ReCaptcha
     *
     * @return void
     */
    function prefix_recaptcha_hook_management() {
    	
    	if( ! is_user_logged_in() ) {
    		return;
    	}
    	
    	// reCaptcha Verification
    	remove_filter( 'wpcf7_spam', 'wpcf7_recaptcha_verify_response', 9 );
    	remove_filter( 'wpcf7_spam', 'iqfix_wpcf7_recaptcha_check_with_google', 9 );
    
    	// reCaptcha Enqueues
    	remove_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 10 );
    	remove_filter( 'wp_enqueue_scripts', 'iqfix_wpcf7_recaptcha_enqueue_scripts', 10 );
    
    	// reCaptcha Footer Javascript
    	remove_action( 'wp_footer', 'wpcf7_recaptcha_onload_script', 40 );
    	remove_filter( 'wp_footer', 'iqfix_wpcf7_recaptcha_callback_script', 40 );
    	
    }
    add_action( 'setup_theme', 'prefix_recaptcha_hook_management', 20 ); // WPCF7 ReCaptcha uses priority 10, so we need to use 10+

    You could add the above 2 functions into your own mini-plugin, into a custom themes functions.php, or more likely into a child theme functions.php. The prefix portion of the functions should be replaced with something unique to your theme or plugin.

    Hopefully that answers your question but should you need any further assistance please reply back to this forum and we may help you further. We’ll mark this thread as resolved for now. Have a wonderful rest of your week!

    Thread Starter nlnieuw7

    (@nlnieuw7)

    Works like a charm!

    Thanks a million, you saved my day!

    you could go the other way and just use shortcodes for logged in or logged out users. That is a little bit tricky since you first have to allow shortcodes inside wpcf7 content by adding following somewhere in functions.php

    
    add_filter( 'wpcf7_form_elements', 'do_shortcode' );
    

    Then you can use any shortcodes that hides content from specific users or roles such as this or this

    You can conditionaly show whole wpcf sections, not limiting to the recaptcha tag

    • This reply was modified 1 year, 9 months ago by .
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Disable reCaptcha for logged-in users’ is closed to new replies.