I am new to wordpress so I will surely need more help than others to understand your answers. So, the question is:
I have this website with a login page on home. Let's supose you are a member. If you want to log in, you should put your username and password, right?
Let's suppose your username is Malcovich. After you put your username and password, you click on "enter the website" button, and immediatly you will be redirected to www.mywebsite/MALCOVICH
How can I do that? How can I redirect people after log in to a specific page that is named as the username they use to access the website?
And also I want to put a voice "PROFILE" in the menu that does the same thing, redirects people to a page named as their username. And I want this button in menu to be seen only by loged in people.
Thank you very much for being my saviors
1 Answer
The login_redirect filter should works (add this code into your theme functions.php):
function my_login_redirect( $redirect_to, $request, $user ) {
$redirect_to = home_url($user->user_nicename);
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
For "PROFILE" in the menu (add this code into your template file where you want to display it):
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
echo '<a href="' . esc_url( home_url( $current_user->user_nicename ) ) . '">PROFILE</a>';
}
UPDATE: If you manage menu from backend, I don't know if any better solution, but this will works:
Add a custom menu item with name "PROFILE", and add this code in your theme functions.php:
function filter_nav_menu_items( $menu ) {
if ( $menu->title == 'PROFILE' ) {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$menu->url = home_url( $current_user->user_nicename );
}
else {
$menu->_invalid = true;
}
}
return $menu;
}
add_filter( 'wp_setup_nav_menu_item', 'filter_nav_menu_items', 999 );
-
Hi, I'm really happy your advice worked perfectly. Now users are redirected to their own page after login. I have another question. Where should I put the code for the profile button in menu? Apr 27, 2018 at 11:59
-