Two functions utilizing registration_errors filter

I wrote two plugins that utilize the registration_errors filter:

add_filter( 'registration_errors', 'process_payment', 10, 3 );

add_filter( 'registration_errors', 'add_user_to_SF', 10, 3 );

When add_user_to_SF returns errors, the process_payment function runs successfully (I know this because it processes a payment).

Read More

How can I set this up so that when one of them returns an error the other doesn’t run and user registration doesn’t happen?

Related posts

Leave a Reply

1 comment

  1. Identifying errors via error code

    Run add_user_to_SF with an earlier priority, to make it execute first

    add_filter( 'registration_errors', 'add_user_to_SF', 9, 3 );`
    

    Let’s assume you have two possible errors in your add_user_to_SF:

    function add_user_to_SF( $errors, $sanitized_user_login, $user_email )
    {
        $has_errors = false;
    
        if ( /* some condition that should throw an error */ ) {
            $errors->add( 'some_error', 'some message' );
            $has_errors = true;
        }
        if ( /* another condition that should throw an error */ ) {
            $errors->add( 'another_error', 'another message' );
            $has_errors = true;
        }
    
        if ( ! $has_errors ) {
            /* write to your external DB */
        }
    
        return $errors;
    }
    

    Then check for those errors in the latter function by using the $errors object’s get_error_codes method:

    function process_payment( $errors, $sanitized_user_login, $user_email )
    {
        $error_codes = $errors->get_error_codes();
    
        if (
            is_array( $error_codes ) &&
            ! empty( $error_codes ) &&
            ! empty( array_intersect( array( 'some_error', 'another_error' ), $error_codes ) )
        ) {
            return $errors;
        } else {
            /* run your payment processing */
        }
    
        return $errors;
    }
    

    Via a flag

    The following is a mock-up of how you’d do it with a flag as a class property:

    if ( ! class_exists( 'WPSE_96362_Registration_Errors' ) ) {
        class WPSE_96362_Registration_Errors
        {
            /* error flag */
            private $has_errors = false;
    
            /* constructor with filters */
            public function __construct()
            {
                /* earlier priority for "add_user_to_SF" method */
                add_filter( 'registration_errors', array( $this, 'add_user_to_SF' ), 9, 3 );
                add_filter( 'registration_errors', array( $this, 'process_payment' ), 10, 3 );
            }
    
            public function add_user_to_SF( $errors, $sanitized_user_login, $user_email )
            {
                if ( /* some condition that should throw an error */ ) {
                     $errors->add( 'some_error', 'some message' );
                     $this->has_errors = true;
                }
                if ( /* another condition that should throw an error */ ) {
                     $errors->add( 'another_error', 'another message' );
                     $this->has_errors = true;
                }
    
                if ( ! $this->has_errors ) {
                    /* write to your external DB */
                }
    
                return $errors;
            }
    
            public function process_payment( $errors, $sanitized_user_login, $user_email )
            {
                if ( $this->has_errors ) {
                    return $errors;
                } else {
                    /* run your payment processing */
                }
    
                return $errors;
            }
        }
    }
    
    $wpse_96362_registration_errors = new WPSE_96362_Registration_Errors();