WordPress menu based on user roles - php

i am new to WordPress and trying to find a way to add menus based on user roles. I have (guest user or non logged in user) vendor and subscriber roles. I want to display different primary menus depending on what role user is. Example code taken from https://wpcodeus.com/display-different-wordpress-menu-to-logged-in-users/
Example code not working
function nav_menu_args( $args = '' ) {
if( is_user_logged_in('vendor')) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'VendorMenu';
}
}
else if
( is_user_logged_in('subscriber')) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'SubscriberMenu';
}
}
else
(!is_user_logged_in) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'PrimaryMenu';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'nav_menu_args' );
At the moment it changes all menus to vendor menu including primary top and footer, all menus have the same menu.
Any help or guidance in the right direction will be much appreciated.
I am adding the code in to the custom/child theme functions.php file.

you can use current_user_can() function to check user capabiltiy or role. see this link.

This worked for me (slightly different method but same outcome)
if ( current_user_can( 'subscriber' ) ) {
//display menu
}
if ( current_user_can( 'vendor' ) ) {
//display menu
}

Related

Hide/Show menus when logged in/out on WordPress

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' );

WORDPRESS astra theme Different header menu for log in and log out user

the question is just on the title, but well here's the problem i tried this code on my function.php which at first works fine, but suddenly i realized that the menus on footer changed to be the same as the header menus.
here is the code :
function wpc_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged-out';
} else {
$args['menu'] = 'Main Menu';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );
What i want is to have a different header menus for user when they log in and log out, but the footer menus stays the same. i am currently learning how to add the conditional logic on the functions.php atm, but if there are other ways please let me know, thx in advance
Probably a bit late for a reply but I just did this in the Astra theme today, so thought I would share the know-how for others who might need it. It can easily be adjusted for any WordPress theme actually:
You can check the location of the menu and only change the menu in the location(s) you decide (in this example 'primary' and 'mobile_menu' which is what Astra theme calls the the main nav locations for desktop and mobile) :
function custom_wp_nav_menu_args( $args = '' ) {
if( $args['theme_location'] == 'primary' || $args['theme_location'] == 'mobile_menu' ) {
if( ! is_user_logged_in() ) {
$args['menu'] = 'logged-out';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_wp_nav_menu_args' );
If you dont want to use the explicit position of the menu (in my case the nav was in a widget in the footer and not a set theme location), you can also do the same thing by using the id, name or slug of the menu. For example if you want to change the menu 'test-menu' for 'test-menu-logged-out' if the user is logged out, you can use this:
function custom_2_wp_nav_menu_args ( $args = '' ) {
$menu_slug = $args['menu']->slug;
if ( ! is_user_logged_in() && $menu_slug == 'test-menu' ){
$args['menu'] = 'test-menu-logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_2_wp_nav_menu_args' );

Wordpress Author box Control Function

Hello friends,
i need little help in Wordpress. I am trying to hide the authorbox that appears under the post for specific user only.
Example if post i am looking into is posted by admin, then i want to hide the authorbox under post content that is posted by admin, but should show for all other users ? i tried different functions but not able to success on this.
I want to completly hide the authorbox, i know it can be done with css code that is
.author-box { display:none;}
but this hides the overall authorbox of complete them, i just want to hide the authorbox on the posts that are made by admin ?
i am using genesis framework, so please suggest if any help you can make here.
Thanks
In your theme's functions.php file, add something like this:
function remove_my_post_metabox() {
global $post;
// If the post author ID is 1, then remove the meta box
// You would need to find the ID of the author, then put it in place of the 1
if ( $post->post_author == 1) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
If you need to find out the role of the author, and truly do it based on the role (admin, for example), then it would be more like this:
function remove_my_post_metabox() {
global $post;
// If there's no author for the post, get out!
if ( ! $post->post_author) {
return;
}
// Load the user
$user = new WP_User( $post->post_author );
// Set "admin" flag
$is_admin = FALSE;
// Check the user roles
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
if ( $role == 'administrator' ) {
$is_admin = TRUE;
}
}
}
// Lastly, if they ARE an admin, remove the metabox
if ( $is_admin ) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );

Displaying Logged-In User Name in Wordpress Menu

I'm using Wordpress with UserPro, and want my menu to display the logged-in user's first name, linked to the user profile page.
The problem is that in my menu structure, the "Profile" menu option is supposed to have a sub-menu containing "edit profile", "submit", and "logout".
This is my current code:
/*earlier code, currently commented out, for function to
display username in menu using #profile_name# placeholder
function give_profile_name($atts){
echo userpro_profile_data('first_name', get_current_user_id());
}
add_shortcode('profile_name', 'give_profile_name');
add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
if ( '#profile_name#' == $menu_item->title ) {
global $shortcode_tags;
if ( isset( $shortcode_tags['profile_name'] ) ) {
// Or do_shortcode(), if you must.
$menu_item->title = call_user_func( $shortcode_tags['profile_name'] );
}
}
}
return $menu_items;
}
end of earlier code */
//currently in use, unlinked code
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
function my_custom_menu_item($items)
{
if(is_user_logged_in())
{
$user=wp_get_current_user();
$name=$user->user_firstname;
$items .= '<li>'.$name.'';
}
return $items;
}
?>
I can fiddle around and try to add the sub-menu under the menu by fiddling with the code from Firebug, but that would mean manual edits to all the links in the functions.php, which would be tedious.
I want to be able to add, edit, remove, and redirect the sub-menu items easily via the Wordpress menu.
Please advise.
Okay, I found a solution (and it can be used for any theme, with any plugin as it only uses core WordPress functions).
In the menu, name the menu item where you want the user's name to appear with a place-holder (such as: #profile_name#, #user#, #random#, etc.)
Now, add the following code to the your child-theme's functions.php:
function give_profile_name($atts){
$user=wp_get_current_user();
$name=$user->user_firstname;
return $name;
}
add_shortcode('profile_name', 'give_profile_name');
add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
if ( '#profile_name#' == $menu_item->title ) {
global $shortcode_tags;
if ( isset( $shortcode_tags['profile_name'] ) ) {
// Or do_shortcode(), if you must.
$menu_item->title = call_user_func( $shortcode_tags['profile_name'] );
}
}
}
return $menu_items;
}
In case you're using your own place-holder, remember to replace #profile_name# with the name of your custom place-holder in the code above.
Apologies in case I've misused the term 'place-holder'.

Can I change a menu after I logged in, in Wordpress?

I've got this wordpress site. And i've made two menu's. One which is supposed to be shown when a user isn't logged in, and one for when a user is logged in. And i'm talking about a login to the website it self, not the dashboard.
How do I make the menu's change, from the one I have where I am not logged in, to the one where I am?
I've tried adding the following code to the functions.php page, but it isn't working.
function my_wp_nav_menu_args( $args = '' ) {
if(session_status() != PHP_SESSION_NONE ) {
$args['menu'] = 'logged-in';
} else {
$args['menu'] = 'logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
I'm not sure where to look next, and I can't find any solutions on google. Hope someone can help me out!
I've also made sure i got the session_start(); at the top of every .php page.
You can make 2 different menus and use a simple if else :
<?php
if ( is_user_logged_in() ) {
wp_nav_menu( array( 'theme_location' => 'registered-menu' ) );
} else {
wp_nav_menu( array( 'theme_location' => 'visitor-menu' ) );
}
?>

Categories