Change WP Menu if Logged In or Out

Snippets

January 1, 1970

This snippet is quite simple, but extremely powerful. It allows you to check if the user is currently logged in or out and change the WordPress menu accordingly.

In my case, the site I used this snippet on was using the default menu via GeneratePress, so it was seamless. All I needed to do was create two WordPress menus, one named “Logged In” and another named “Logged Out.” I then added the links I wanted in each menu, which then changed automatically based on whether the user was logged in or out.

This is extremely powerful for membership or directory sites where it doesn’t make sense to keep the same navigation.

add_filter( 'wp_nav_menu_args', function ( $args ) {
    if ( 'primary' === $args['theme_location'] ) {
        if ( is_user_logged_in() ) {
            $args['menu'] = 'Logged In';
        } else {
            $args['menu'] = 'Logged Out';
        }
    }
    return $args;
} );

Join the Inside Link Community

Get access to all courses, weekly office hours, live build sessions, and an active community of WordPress professionals.

Learn More →