Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Hide Menu/Submen per user role WordPress

  • SOLVED

Hi! I want to hide Menu and Submenu per user role.

I just create this example of php (functions.php) to hide menu-submenus for Contributors; but I could not make it work all, help in need it!!!

(I could make hide menus per role, not submenus, but affecting some page styles; this makes child submenus of already deleted menus to be displayed as menus) :



add_action('admin_menu', 'remove_menus');

function remove_menus() {

//get info user level
global $current_user;
get_currentuserinfo();

//ID of menu item to be removed of logged user as Contributors
if ($current_user->user_level = 6)
removed_menu_items ('222','104');


}

//Remove por submens

add_action('admin_menu', 'remove_submenus');

function remove_submenus() {

//get info user level
global $current_user;
get_currentuserinfo();

//ID of submenu Item to be removed of logged user as Contributors
if ($current_user->user_level = 6)
removed_submenu_items ('216','259','205','234');
}

Answers (1)

2011-03-10

Sébastien | French WordpressDesigner answers:

Miguel,

you must add this function in your file functions.php

class menu_item_by_role extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

$class_names = $value = '';

$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';

$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';

/*-------Here begins the code you must customize---------*/
global $current_user;

//the line below means : if the current user is an administrator, he can't see the menu_item 22 and the menu_item 21
if(($current_user->allcaps['administrator'])AND(($item->ID==22)OR($item->ID==21))){$output.="";}

//if a line is not needed now, add this sign at the beginning of the line, like below
//elseif($current_user->allcaps['editor']){$output.="";}
//elseif($current_user->allcaps['author']){$output.="";}

//the line below means : if the current user is an contributor, he can't see the menu_item 537 and the menu_item 539 and the menu_item 538
elseif(($current_user->allcaps['contributor'])AND(($item->ID==537)OR($item->ID==539)OR($item->ID==538))){$output.="";}

//the line below means : if the current user is an contributor, he can't see the menu_item 538 and the menu_item 540 and the menu_item 541
elseif(($current_user->allcaps['subscriber'])AND(($item->ID==538)OR($item->ID==540)OR($item->ID==541))){$output.="";}

/* end of the code you must customize*/

else {
$output .= $indent . '<li' . $id . $value . $class_names .'>';

$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';

$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;

$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
}


you must change the code to display your menu
you use a code probably like that :
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>
replace it by this
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary','walker' => new menu_item_by_role() ) ); ?>
As you can see, you must just add 'walker' => new menu_item_by_role()

if your code is :
<?php wp_nav_menu(); ?>
it become
<?php wp_nav_menu( array( 'walker' => new menu_item_by_role() ) ); ?>


EDIT : miguel, here is a problem here : i can't post a respons because "This question has completed.". I can just post here : i try to send you an email :-)


miguel angarita comments:

Yes I can if you know how to answer it.-



Sébastien | French WordpressDesigner comments:

That's a really job... do you increase or not ?


miguel angarita comments:

Yes. will be increased.


Sébastien | French WordpressDesigner comments:

ok, i will post the code when the fee will be increased.


Sébastien | French WordpressDesigner comments:

OK, i have a demo :

here i have a menu with 6 items : [[LINK href="http://demo1.archiparmentier.com/"]]http://demo1.archiparmentier.com/[[/LINK]]



the code is like that :

if(($current_user="administrator")AND(($item_ID==539)OR($item_ID==538))){
//item not displayed}
elseif(($current_user="subscriber")AND(($item_ID==540)OR($item_ID==541))){
//item not displayed}



if you are not connected you can see 6 items
if you are subscriber, you don't see the item 540 and 541
if you are contributor, you don't see the item 539 and 538

[[LINK href="http://demo1.archiparmentier.com/wp-login.php"]]http://demo1.archiparmentier.com/wp-login.php[[/LINK]]
to connect you like subscriber :
id=subscriber
psw=subscriber

to connect you like contributor :
id=contributor
psw=contributor

I leave this demo up for half an hour. Look at this.

I post the real code when the fee is increased.


Sébastien | French WordpressDesigner comments:

Ok, you have no increased the fee, i suppose you have find the response. I remove the demo. Bye.


miguel angarita comments:

Dear Sebastien. I will update tomorrow the fee not worries. Right now I am working and do not have a secure connection to authorize my bank payment-.


miguel angarita comments:

I just see you saw your post but cannot enter now at demo site- Could you activate it now to see how this works.

Thanks


miguel angarita comments:

Sorry I was working now have time to see your demo and tonight I will ingrease payment. No worries.


Sébastien | French WordpressDesigner comments:

ok, i have update the demo


miguel angarita comments:

Great!!


miguel angarita comments:

OK Sebastien I saw it. But can you close Item 536 and 537 for subscriber and contributor, respectively?

You can close demo now if you want, and reopened later.


Sébastien | French WordpressDesigner comments:

close item 536 for subscriber and close item537 for contributor, that's it ?


miguel angarita comments:

Yes. We need to do both. ( what you have done already regarding hide submenu for roles).and we need as well to hide menu same way- Can you do it in demo?


Sébastien | French WordpressDesigner comments:

the demo is updated


miguel angarita comments:

let me see


miguel angarita comments:

Thanks Sébastien. You have done this. Congratulations. Can close demo now- I will update payment tonight so you can come back tomorrow. Do you have future availability for other asignments?


Sébastien | French WordpressDesigner comments:

yes i have.

Here is the midnight, i come back tomorrow


Sébastien | French WordpressDesigner comments:

Miguel, i add the code in my first post, because that the one i can edit, if i want change anything.


miguel angarita comments:

Two questions.

1. What name is the file.php that contains the "you must change the code to display your menu you use a code probably like that " that you said?

2.What do you mean by "As you can see, you must just add 'walker' => new menu_item_by_role(). Where is the file .php located. \


Thanks



miguel angarita comments:

My email is [email protected]


miguel angarita comments:

Sebastien please contact me:

I just copy the code and found at function.php this:

function cloudfw_add_homepage_item_to_nav_menu($menuItems, $args)
{
if('primary' == $args->theme_location)
{
if ( is_front_page() )
$class = 'class="current-menu-item"';
else
$class = '';

$homeMenuItem = '<li ' . $class . '>' .
$args->before .
'<a href="' . home_url( '/' ) . '" title="'.__('Home','cloudfw').'">' .
$args->link_before .
'<span>'.__('Home','cloudfw').'</span>' .
$args->link_after .
'</a>' .
$args->after .
'</li>';

$menuItems = $homeMenuItem . $menuItems;
}

return $menuItems;
}

//add_filter( 'wp_nav_menu_items', 'cloudfw_add_homepage_item_to_nav_menu', 10, 2 );

but your line that you says:

<?php wp_nav_menu…..

is commented.