Wp logout not redirecting to homepage

When I run the following code;

add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'primary') {
    $items .= '<li><a href="'.wp_logout_url().'" title="Logout">Logout</a></li>';
}					
elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
    $items .= '<li><a href="http://localhost/beta/planandteach/members/login">Log In</a></li>';
}
return $items;

}

The log out link takes me to the admin login page - I thought using url() would have taken me to the homepage?

Take a look at the WordPress documentation for that function. Basically, without parameters, it will log the user out in place. If you logout in a users-only area, it’ll take you to the login page for that area.

Here is Wordpress’ suggested link for redirecting to the homepage:
<a href="<?php echo wp_logout_url( home_url() ); ?>">Logout</a>
So in your case, you’d want to adjust that, of course, to something like

if (is_user_logged_in() && $args->theme_location == 'primary') {
    $items .= '<li><a href="'.wp_logout_url(home_url()).'" title="Logout">Logout</a></li>';
}
  • just adding the home_url() parameter.
1 Like

Thanks I actually worked it out on reading the codex - sorry for wasting your time as I tried to respond here but the admin blocked me out for a spam reason or something. Great answer though so hopefully someone else benefits.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.