PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

Hide menu items based on s2 level

s2Member Plugin. A Membership plugin for WordPress®.

Hide menu items based on s2 level

Postby man-O-media » 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:
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&#47;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
User avatar
man-O-media
Experienced User
Experienced User
 
Posts: 117
Joined: February 10, 2011

Re: Hide menu items based on s2 level

Postby Cristián Lávaque » August 6th, 2011, 12:24 am

Thanks for sharing that, DP. I'm sure it'll help others do that. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010


Return to s2Member Plugin

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 2 guests

cron