• Hello wordpress community. In my website there are 2 plugins that use Captcha. One of them is WPForms, I set it up with Captcha and it worked great. However, after installing another plugin, which required adding additional code to functions.php(basically adding captcha functionality) for Captcha to work, it broke WPForms form, the form could not been submitted, because captcha loaded multiple times. How could such problems be fixed? Both plugins uses version 2.0 captcha, would using 3.0 on one plugin fix the issue?

    Code added to functions.php in theme folder:

    
    /* ADD RECAPTCHA in Job Detail to Application From */
    
    // Define your keys here
    define( 'RECAPTCHA_SITE_KEY', 'your_site_key' );
    define( 'RECAPTCHA_SECRET_KEY', 'your_secret_key' );
    // Enqueue Google reCAPTCHA scripts
    add_action( 'wp_enqueue_scripts', 'recaptcha_scripts' );
    function recaptcha_scripts() {
    	wp_enqueue_script( 'recaptcha', 'https://www.google.com/recaptcha/api.js' );
    }
    // Add reCAPTCHA to the job submission form
    // If you disabled company fields, the submit_job_form_end hook can be used instead from version 1.24.1 onwards
    add_action( 'job_application_form_fields_end', 'recaptcha_field' );
    function recaptcha_field() {
    	?>
    	<fieldset>
    		<label>Please validate the form</label>
    		<div class="field">
    			<div class="g-recaptcha" data-sitekey="<?php echo RECAPTCHA_SITE_KEY; ?>"></div>
    		</div>
    	</fieldset>
    	<?php
    }
    // Validate
    add_filter( 'application_form_validate_fields', 'validate_recaptcha_field' );
    function validate_recaptcha_field( $success ) {
    	$response = wp_remote_get( add_query_arg( array(
    		'secret'   => RECAPTCHA_SECRET_KEY,
    		'response' => isset( $_POST['g-recaptcha-response'] ) ? $_POST['g-recaptcha-response'] : '',
    		'remoteip' => isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']
    	), 'https://www.google.com/recaptcha/api/siteverify' ) );
    	if ( is_wp_error( $response ) || empty( $response['body'] ) || ! ( $json = json_decode( $response['body'] ) ) || ! $json->success ) {
    		return new WP_Error( 'validation-error', 'Please try again' );
    	}
    	return $success;
    }
    

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter ppaulikelis

    (@ppaulikelis)

    I can not recall exactly what error said specifically, but it was something about captcha loading multiple times. My research in the internet suggests that and because of that captcha does not work.

    Hi,

    can you try removing following code from functions.php once:

    define( ‘RECAPTCHA_SITE_KEY’, ‘your_site_key’ );
    define( ‘RECAPTCHA_SECRET_KEY’, ‘your_secret_key’ );
    // Enqueue Google reCAPTCHA scripts
    add_action( ‘wp_enqueue_scripts’, ‘recaptcha_scripts’ );
    function recaptcha_scripts() {
    wp_enqueue_script( ‘recaptcha’, ‘https://www.google.com/recaptcha/api.js&#8217; );
    }

    Thread Starter ppaulikelis

    (@ppaulikelis)

    Hi,

    Thanks for replying.

    Removing that part allows for WPForms to work, however, another plugin then fails to load captcha, it does not appear at all. Just checked it :\

    Thread Starter ppaulikelis

    (@ppaulikelis)

    Any other solutions?

    Sure, I would have to try changing code, can i get ftp access to website, you can share details to my email: [email protected]

    also, please share links to both pages..

    Thread Starter ppaulikelis

    (@ppaulikelis)

    Hello,

    Thanks for replying again. I am not comfortable sharing this kind of information. I can send you some sort of build of this website with no information/pages/posts and only essential things for you to test.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Captcha loads 2 times’ is closed to new replies.