I want to change menu label of items accordingly if a user is logged in or not .
If user is not logged in i want to show Login and if user is admin I want to show Admin page and if normal user is logged in i want to show My Profile page .
I have tried this code.
add_filter( 'wp_nav_menu_items', 'dynamic_label_change', 10, 2 );
function dynamic_label_change( $items, $args )
{
if (!is_user_logged_in() && $args->theme_location == 'topbar_navigation')
{
$items = str_replace("Login", "Profile", $items);
}
return $items;
}
If you want to add new menu item then below code will help.
// Add Login / Logout menu item dynamically to primary navigation menu
function custom_menu_links( $items, $args ) {
if ($args->theme_location == 'primary'){
if (is_user_logged_in()) {
$items .= '<li>Logout</li>';
} else {
$items .= '<li>Login</li>';
}
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'custom_menu_links', 10, 2 );
Related
My website is built with WordPress and I currently have 3 nav menus
The main menu - "Main"
A secondary top menu - "Player Logged-in"
Another secondary top menu - "Player Logged-out"
Ive added the script below to my functions.php
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = "Player Logged-in";
} else {
$args['menu'] = "Player Logged-out";
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
It is working, the only problem is it replaces my "Main" menu with one of the secondary menus.
How would I go about it if I wanted the 2 secondary top menus to alternate if a user is logged in/out AND I wanted the "Main" menu to stay regardless of whether the user is logged in or not?
Thanks
I came right with this problem.
I looked for the specific menu locations (top_navigation) for my theme (Avada) and used this
function wpc_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in()) {
if( 'top_navigation' == $args['theme_location'] ) {
//top_navigation is specific to Avada in my case
$args['menu'] = 'Player Logged-in';
}
} else {
if( 'top_navigation' == $args['theme_location'] ) {
//again, top_navigation is specific to Avada in my case
$args['menu'] = 'Player Logged-out';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );
if (!current_user_can('administrator')) {
function remove_admin_menus () {
global $menu;
$removed = array(
__('WooCommerce'),
);
end ($menu);
while (prev($menu)){
$value = explode(
' ',
$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $removed)){
unset($menu[key($menu)]);
}
}
}
}
add_action('admin_menu', 'remove_admin_menus');
This code hide the whole Woocommerce item from Wordpress dashboard, if you are Administrator, but i didn't fiind a solution to hide only Orders sub-menu, not the whole item.
Who has an idea?
You were taking global $menu instead $submenu. Then you will get a list of all submenus registered. You can add the following code. Also it is better to check if user is admin inside the function call
function remove_admin_menus(){
global $submenu;
if(current_user_can('administrator')){
unset($submenu['woocommerce']['1']);
}
}
add_action('admin_menu', 'remove_admin_menus');
UPDATE
Even if the menu is hidden, one can access the page if he knows the url. So inorder to block the access to url, add the following
function restrict_woo_submenu_userrole(){
$current_screen = get_current_screen();
$p_id = $current_screen->id;
if($p_id == 'edit-shop_order' && current_user_can('administrator')){
wp_die('Restricted Access.');
}
}
add_filter( 'current_screen', 'restrict_woo_submenu_userrole' );
I am working out of WordPress, I have inserted a custom menu in the header.php file in my theme.
I now dont have access to my menu through WordPress appearance >menus on my dashboard..
Normally , a plug in can make certain menu links visible to select role or users.
But i have no access there .
How can I control the menu to show to certain users or by a specific role (using CODE ONLY)?
Instead of adding "Log In" and "Log Out" to the menu, you add a filter to your functions.php which checks if the user is logged in or not, then displays the appropriate link:
add_filter( 'wp_nav_menu_items', 'xyz_add_members_to_menus', 10, 2 );
function xyz_add_members_to_menus( $items, $args ) {
if( 'primary' === $args->theme_location ) {
if (is_user_logged_in()) {
$items .= '<li>Log Out</li>';
} else {
$items .= '<li>Log In</li>';
}
}
return $items;
}
I'm trying to add an item to a sub menu via functions.php as the item will change based on the users logged in status and subscription status.
Here is the code I use to add an item to the menu:
add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {
if (is_single() && $args->theme_location == 'primary') {
$items .= '<li>Show whatever</li>';
}
return $items;
}
However, does anyone know how I would add an item to a submenu?
You can use add_menu_subpage wordpress' function.
add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
add_submenu_page(
null //or 'options.php'
, 'My Custom Submenu Page'
, 'My Custom Submenu Page'
, 'manage_options'
, 'my-custom-submenu-page'
, 'my_custom_submenu_page_callback'
);
}
Reference: http://codex.wordpress.org/Function_Reference/add_submenu_page
I have a page called edit profile, i have installed the profile builder plugin,so the users can edit their profile from front end, if the user is logged in i wants to show the edit profile and logout links in the menu else it is login. how can i achieve that? i am using wp-bootstrap responsive theme, i am new in wordpress development any one please help me.Do i need to change any thing in the header.php file?
<?php
if ( is_user_logged_in() ) {
// i want to add Edit profile and logout to the menu
} else {
// i want to add login to the menu
} ?>
You can use the wp_nav_menu_items filter to add the menu
add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {
if ( is_user_logged_in() ) {
$items .= 'Show whatever';
} else {
$items .= 'Show whatever';
}
return $items; /* This will have the menu items */
}
Reference