Site logo

Archived topic

Changing link based on logging in

6 replies · Started by William on October 14, 2021

Viewing posts 1–7 of 7

Hi there,

Is there a way to have a link appear in the main menu of GeneratePress, and then, once that user is logged in, for the link to change to another link?

Kind regards,

Will

Hi William,

If you are adding the link using Hooks, then the easier way is to add link-A to Hook A, choose logged out for users option.

Add link-B to Hook B, choose logged in for users option.

So link A can only been seen by logged out user, once they are logged in, they can see link B.

Let me know if this helps :)

That sounds good, thank you!

How do I go about doing this?

So I have two links in the main navigation menu - 'Login' and 'Read Now'

Hi there,

you can use a snippet like this:

function add_login_logout_register_menu( $items, $args ) {
  $login = home_url( '/loggedin_url/' );
  $logout = home_url( '/loggedout_url/' );
  if ( 'primary' !== $args->theme_location ) {
    return $items;
  }
  if ( is_user_logged_in() ) {
    $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children"><a href="' . $logout . '">Logged In Label</a></li>';
  }
  else {
    $items .= '<li><a href="' . $login . '">Logged Out label</a></li>';
  }
  return $items;
} 
add_filter( 'wp_nav_menu_items', 'add_login_logout_register_menu', 100, 2 );

This is absolutely perfect, thank you so much!

Glad to be of help!

This archived topic is closed to new replies.