0

I try to hook in user creation process in admin dashboard /wp/wp-admin/user-new.php to make some custom validation on data with the following hook, but the hook is not executed on form submit. What goes wrong in this case?

What I want, I have a custom ACF relation field which should be mandatory only if the selected role is editor. So I want to parse the request and check against the rules. If it fails than do not create the user return the custom error message.

add_action( 'register_post', function($user_login, $user_email, $errors) {
    var_dump('test');
    die;

    $userIsValid = ValidateUser::make($errors);

    if(!$userIsValid) {
        $errors->add( 'bad_email_domain', '<strong>ERROR</strong>: errors' );
    }
});
11
  • 1
    There's a lot of missing information and context here, can you update your question to include the form, add some context for what ValidateUser is and what is inside ValidateUser::make, or why you're hooking into register_post? Use the Edit link to add more details to your question
    – Tom J Nowell
    Commented Jun 16, 2022 at 13:00
  • Hi @TomJNowell I just made an update on post
    – fefe
    Commented Jun 16, 2022 at 13:08
  • 1
    ah I see, keep in mind that you can't ask about the ACF part here as 3rd party plugin dev support questions are offtopic here, but a general how to run code before a user has been created and block the creation could be asked. It's definitely not register_post though, users are not posts.
    – Tom J Nowell
    Commented Jun 16, 2022 at 13:18
  • As per documentation seems like to be used before a user is created developer.wordpress.org/reference/hooks/register_post . Can you please give some tips which hooks are appropriate to use for this scenario?
    – fefe
    Commented Jun 16, 2022 at 13:22
  • 1
    @fefe I have just checked and register_post only fires for user registrations via the WordPress login page. You should use the user_profile_update_errors hook instead. Commented Jun 16, 2022 at 13:57

1 Answer 1

0

You need to add , 10, 3 after your closure to set the hook priority and (more importantly) the number of accepted arguments - otherwise $user_email and $errors will not be passed.

add_action( 'register_post', function ( $user_login, $user_email, $errors ) {
    $userIsValid = ValidateUser::make($errors);

    if(!$userIsValid) {
        $errors->add( 'bad_email_domain', '<strong>ERROR</strong>: errors' );
    }
}, 10, 3 );

Update: The register_post hook only fires inside register_new_user(), which is only used for user registration via the WordPress login page.

To handle errors within the admin (i.e. Users > Add New) use the user_profile_update_errors hook instead.

1
  • thank you for your feedback, seems not to be triggered on user creation even with the closures
    – fefe
    Commented Jun 16, 2022 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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