通过错误代码识别错误运行add_user_to_SF
使用较早的优先级,使其首先执行
add_filter( \'registration_errors\', \'add_user_to_SF\', 9, 3 );`
假设您的
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;
}
然后使用
$errors
对象的
get_error_codes
方法:
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;
}
通过标志
以下是如何将标志作为类属性的模型:
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();