Hide menu items based on s2 level
Posted: August 4th, 2011, 1:37 pm
Hi all...
It is great to have content which can only be accessed by specific users while those that don't have access are redirected to the options page where they can gain access but I have some content which needs to be really easy to access by authoized users and not visible to others.
So while I am still only scratching the surface of what s2 can do, and am also pretty limited in my php capability, by searching out various code bits I have put together a way to hide menu items based on their s2 member level.
In my case the theme I am using has two registered menus, a full "main menu" bar with drop downs and a simple "top menu" line of a few items which is the one I am modifying.
The basic idea is if the menu is the one we want and the user has the correct s2 level we add the extra links (I also add login/register), otherwise we just show the menu items which are added on the appearance page and I also add the log out link.
To determine what menus you actually have registered, look for his function: register_nav_menus in functions.php
This is what mine looks like:
This is the code to modify the menu which I have at the bottom of functions.php.
Note that the class="home" is actually a carry over from the original code which I didn't bother removing.
Perhaps there is a better way to do this and I would be especially interested in a way to inject these changes into the middle of a main menu bar with drop downs, etc. so the links could be added as sub menu items.
The only idea I have at the moment is a string function which finds specific text and rebuilds the string with the new items inserted but I have not yet tried to figure it out.
I hope it helps someone.
DP
It is great to have content which can only be accessed by specific users while those that don't have access are redirected to the options page where they can gain access but I have some content which needs to be really easy to access by authoized users and not visible to others.
So while I am still only scratching the surface of what s2 can do, and am also pretty limited in my php capability, by searching out various code bits I have put together a way to hide menu items based on their s2 member level.
In my case the theme I am using has two registered menus, a full "main menu" bar with drop downs and a simple "top menu" line of a few items which is the one I am modifying.
The basic idea is if the menu is the one we want and the user has the correct s2 level we add the extra links (I also add login/register), otherwise we just show the menu items which are added on the appearance page and I also add the log out link.
To determine what menus you actually have registered, look for his function: register_nav_menus in functions.php
This is what mine looks like:
- Code: Select all
register_nav_menus(
array(
'main-menu' => __( 'Main Menu' ),
'top-menu' => __( 'Top Menu' )
This is the code to modify the menu which I have at the bottom of functions.php.
Note that the class="home" is actually a carry over from the original code which I didn't bother removing.
- Code: Select all
<?php
// Filter wp_nav_menu() to add links or text
function new_nav_menu_items($items,$args)
{
if( $args->theme_location == 'top-menu' ) //if we have the top menu which is the registered menu name in my theme
{
if (is_user_logged_in())
{
if (current_user_can("access_s2member_level2")) // content for Members who are logged in with an s2Member Level >= 2.
{
$homelink2 = '<li class="home"><a href="http://mydomain.com/wp1/the-page-to-link-to/" >The link name</a></li>'.'<li class="home"><a href="http://mydomain.com/wp1/the-page-to-link-to/" >The link name</a></li>'.;
$items = $homelink2 . $items;
}
$homelink = '<li class="home"><a href="http://mydomain.com/wp1/wp-login.php?action=logout" >Logout</a></li>'.'<li class="home"><a href="http://mydomain.com/wp1/profile-page" >My Profile</a></li>';
$items = $homelink . $items;
}
else
{
$homelink = '<li class="home"><a href="http://mydomain.com/wp1/wp-login.php" >Login/register</a></li>';
$items = $homelink . $items;
}
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items',10,2);
?>
Perhaps there is a better way to do this and I would be especially interested in a way to inject these changes into the middle of a main menu bar with drop downs, etc. so the links could be added as sub menu items.
The only idea I have at the moment is a string function which finds specific text and rebuilds the string with the new items inserted but I have not yet tried to figure it out.
I hope it helps someone.
DP