Skip to content

Instantly share code, notes, and snippets.

@VlooMan
Created June 7, 2019 07:56
Show Gist options
  • Select an option

  • Save VlooMan/b2f99357c8fd90c91ffc82b0d88003a9 to your computer and use it in GitHub Desktop.

Select an option

Save VlooMan/b2f99357c8fd90c91ffc82b0d88003a9 to your computer and use it in GitHub Desktop.

Revisions

  1. VlooMan created this gist Jun 7, 2019.
    31 changes: 31 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <?php

    /**
    * This piece of code fixes the "No CAPTCHA reCAPTCHA for WooCommerce" plugin's incorrect code when generating
    * lost password link. You can copy it to functions.php of your theme or child theme until the plugin developer fixes
    * the plugin behavior.
    */

    // Add our fixed version only if the original filter is hooked already.
    if ( has_filter( 'allow_password_reset', array( 'WC_Ncr_Lost_Password_Captcha', 'validate_lost_password_captcha' ) ) ) {
    add_filter( 'allow_password_reset', 'my_theme_prefix_validate_lost_password_captcha', 11 );
    }

    function my_theme_prefix_validate_lost_password_captcha( $allow ) {

    // Only run if password reset form sent and captcha response exists.
    if ( isset( $_POST['wc_reset_password'] ) && isset( $_POST['g-recaptcha-response'] ) ) {

    // Since we are running the filter with priority 11 it will run after the first time of the original validation.
    // We want to then remove the original filter as it will be called 2nd time when generating the reset link and
    // the captcha key will already be invalid as already validated the first time.

    if ( $allow ) {
    // Remove the broken validation hook from the plugin.
    remove_filter( 'allow_password_reset', array( 'WC_Ncr_Lost_Password_Captcha', 'validate_lost_password_captcha' ) );
    }

    }

    return $allow;
    }