I need to redirect user after registration to their author page.
The filter 'registration_redirect'
is working, I just can’t seem to retrieve the recently registered user ID in order to get their author page and redirect them.
I am trying to get the recently registered user ID from $_POST
, wp_get_current_user()
, $_GET
, however, no luck.
add_filter( 'registration_redirect', 'redirect_to_author_page' );
function redirect_to_author_page( $registration_redirect ) {
// this is not working
$user = wp_get_current_user();
$user_id = $user->ID; // EMPTY
// this is not working either
$user = get_user_by( 'email', $_POST['user_email'] );
$user_id = $user->ID; // EMPTY
$author_url = get_author_posts_url( $user_id );
return $author_url;
}
Any idea how could I retrieve the recently registered user ID in the ‘registration_redirect’ filter?
Any other way I could accomplish this? (Other filter or hook perhaps)?
Thank you
2
Answers
If you check the documentation’s page for the registration_redirect filter, you’ll notice a second parameter called $errors.
It’s not evident by its name, but, if the registration was successful, this parameter contains the user ID.
So your code should look like this:
An alternative hook to use is register_new_user as its specific to the
wp-login.php
form and receives the newly registered$user_id
, eg:Even though the documentation for
registration_redirect
states it should return$user_id
on success, I tested and found it still containsWP_Error
in WP 6.3 when called after a successful registration.Using
register_new_user
won’t interfere with the forms error handling; the redirection only occurs after a new user is successfully registered.