1

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).

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?

  • 1
    If priority and passed parameters are the same, why not simply chain the functions and have a single filter callback? – Johannes Pille Apr 18 '13 at 20:33
  • Because one of them uses a Salesforce library to enter user data to an external db and the other uses a payment gateway. I prefer not to mix them together. I am willing to change priority or passed parameters, can you tell me more about that? – MF1 Apr 18 '13 at 20:35
  • The third and fourth parameter of add_filter are the priority (0 runs first) in which the filter callbacks are run and the amount of parameters passed to the respective callback. Changing that will not help your issue - I was mentioning the two, because if you had chosen different values, I would have understodd why you'd need to have two separate filter callbacks. This way I didn't/don't. – Johannes Pille Apr 18 '13 at 20:39
  • 1
    I'd put both functions inside a class and give the class a boolean flag property. Run the validation (?!) method, i.e. add_user_to_SF, first, set the flag if errors are present and make the second one depend on the flag. – Johannes Pille Apr 18 '13 at 20:51
2

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();
  • thank you - but my functions are in separate plugins... meaning in separate files and folders... – MF1 Apr 19 '13 at 1:00
  • Well, do it by checking for the existence of your error codes from the first function by means of the get_error_codes method of the $errors object, both plugins will be using the same instance of it after all. That will work for your scenario. Still, if you have to have the two depend on each other, it makes little sense to put them in separate plugins. I'd call that an architectural flaw. – Johannes Pille Apr 19 '13 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.