Halloween Sale 15% off Ends in See the plans
PHP

Different Menu for Logged-In Visitors

Shows another navigation menu to logged-in users in the same theme location, for example with "My account" and "Log out" instead of "Sign up".

Don’t have WPIDE yet? Install it on your site

Code

// Theme location to swap and the menu to show to logged-in users.
$wpide_member_menu_location = 'primary';
$wpide_member_menu          = 'Members Menu';

add_filter( 'wp_nav_menu_args', function ( $args ) use ( $wpide_member_menu_location, $wpide_member_menu ) {
    if ( ! is_user_logged_in() || empty( $args['theme_location'] ) || $wpide_member_menu_location !== $args['theme_location'] ) {
        return $args;
    }

    if ( wp_get_nav_menu_object( $wpide_member_menu ) ) {
        $args['menu'] = $wpide_member_menu;
    }

    return $args;
} );

Notes

Create the menu under Appearance > Menus and put its name or slug in $wpide_member_menu. Works with classic themes.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.