user menu

Home » Snippets » user menu
0

Created with:

Visibility: 

public

Creator: alexben575

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
// Display user information in navigation menu
function user_menus_show_info($info, $user) {
    // Customize the user information to display in the menu
    $info = 'Hello, ' . $user->display_name;
    return $info;
}
add_filter('user_menus_show_info', 'user_menus_show_info', 10, 2);

// Add login, register, and logout links to the menu
function user_menus_add_links($items, $args) {
    if ($args->theme_location == 'primary') { // Change 'primary' to your menu location
        if (!is_user_logged_in()) {
            // Add login and register links for logged-out users
            $login_link = wp_loginout(home_url(), false);
            $register_link = '<a href="' . esc_url(wp_registration_url()) . '">Register</a>';

            $items .= '<li class="menu-item menu-item-login">' . $login_link . '</li>';
            $items .= '<li class="menu-item menu-item-register">' . $register_link . '</li>';
        } else {
            // Add logout link for logged-in users
            $logout_link = '<a href="' . esc_url(wp_logout_url(home_url())) . '">Logout</a>';

            $items .= '<li class="menu-item menu-item-logout">' . $logout_link . '</li>';
        }
    }
    
    return $items;
}
add_filter('wp_nav_menu_items', 'user_menus_add_links', 10, 2);
				

Explanation:

The code above integrates the User Menus plugin features into your theme. It adds a filter to customize the user information displayed in the menu and a filter to add login, register, and logout links to the navigation menu.

In the user_menus_show_info function, you can customize the user information that is displayed in the navigation menu. The example code displays a simple greeting message including the user's display name.

In the user_menus_add_links function, the code checks if the menu being rendered is the primary menu (change 'primary' to the location of your theme's primary menu). For logged-out users, it adds login and register links to the menu. For logged-in users, it adds a logout link to the menu.

Please note that the code assumes your theme uses the wp_nav_menu function to display the menu. Make sure to replace 'primary' with the correct location of your theme's primary menu. You can also modify the code to fit the structure and styling of your theme.

By adding this code to your theme's 'functions.php' file, the User Menus plugin features will be integrated into your theme, allowing you to control menu visibility, display user information, and add login, register, and logout links to your menu.

Register an account to save your snippets or go Pro to get more features.