Member Links Filter Advanced Demo

Here is an advanced demonstration of what you can do with the links filter hooks in WP-Members.  For this example, we are going to add some content to the Members Area page (created with the [wp-members page="members-area"] shortcode. 

We will be making use of several WP native functions. It is important to remember that there is a lot of power in WP. Familiarizing yourself with the WP Codex is definitely recommended if you want to be able to leverage the power of WordPress without having to reinvent the wheel (something I see A LOT of).

Member Links DemoWhat we will create in this example will add the username, a gravatar image, some and some registration details to our members area page.  It will look something like the image here.  For the example we will be using the TwentyTen theme.

Adding a filter

To add a filter, generally you will add some code to your theme’s functions.php file.  Open that up to get started.

We will add the wpmem_members_links filter and apply a new function to filter the generated content (which amounts to the bulleted links you normally see).

add_filter( 'wpmem_member_links', 'my_member_links' );
function my_member_links( $links )
{
  return $links;
}

So the filter will fire the function “my_member_links”, which is where we will filter the generated content, which we are passed to the function.  We are calling those $links.  Right now, our function doesn’t do anything except return the $links as they were – unfiltered.

Let’s add some content

We want to show some things like username and maybe a gravatar.  We can get most of what we need with the WordPress function called get_currentuserinfo which puts the current users information into the $current_user object.  That will get us things like the username and anything directly stored in the wp_users table (like the user’s email and website). This is one reason why the WP-Members plugin includes the native WP user fields.

add_filter( 'wpmem_member_links', 'my_member_links' );
function my_member_links( $links )
{
   // get the current_user object
   global $current_user;
   get_currentuserinfo();

   // show the user info
   $str = '<div id="theuser">
       <h3 id="userlogin"><span style="color: white">' . $current_user->user_login . '</span></h3>
       <dl id="userinfo">
       <dt>Member Since</dt>
       <dd>' . $current_user->user_registered . '</dd>
       <dt>Website</dt>
       <dd><a class="url" href="' . $current_user->user_url . '" rel="nofollow">' . $current_user->user_url . '</a></dd>
       </dl>
     </div>
     <hr />';

   // tag the original links on to the end
   $string = $str . $links;

   // send back our content
   return $string;
}

OK. Now our filter function is starting to take on some shape.  You can see now how we are using the $current_user object to display native user data.

Now we’ll add a gravatar using the WP function get_avatar (and assuming you have your WP settings set to use gravatar images).  And the date is rather ugly when displayed raw and unformatted, so we’ll break that out and format it with the php date function.  We will also get the user’s city and state that is stored in the wp_usermeta table using get_user_meta.

This is probably a good place to remind you that any of the WP-Members custom fields are also stored in the wp_usermeta table.  You can retrieve any of these values using get_user_meta where the meta key is the option name from the WP-Members fields table.

Here is our function now utilizing these additional elements:

add_filter( 'wpmem_member_links', 'my_member_links' );
function my_member_links( $links )
{
// get the current_user object
global $current_user;
get_currentuserinfo();

     // format the date they registered
$regdate = strtotime( $current_user->user_registered );

// and the user info
$str = '<div id="theuser">
<h3 id="userlogin"><span style="color: white">' . $current_user->user_login . '</span></h3>
        <div id="useravatar">' . get_avatar( $current_user->ID, '82' ) . '</div>
          <dl id="userinfo">
          <dt>Member Since</dt>
          <dd>' . date( 'M d, Y', $regdate ) . '</dd>
          <dt>Website</dt>
          <dd><a class="url" href="' . $current_user->user_url . '" rel="nofollow">' . $current_user->user_url . '</a></dd>
          <dt>Location</dt>
          <dd>'
            . get_user_meta( $current_user->ID, 'city', true )
            . ', '
            . get_user_meta( $current_user->ID, 'thestate', true )
            . '</dd>
          </dl>
        </div>
        <hr />';

     // tag the original links on to the end
     $string = $str . $links;

     // send back our content
     return $string;
}

Now we our filter is starting to look like something.  You could really use it as is here.  But let’s pretty it up a little bit first.

Apply some style

We’ll add a little CSS to our HTML output here just to give this some nicer looking layout.

[Click to get a phps file with the style formatting I used in the example screenshot.]

Well, that’s it.  You’ve learned how to use some native WordPress functions to pull user data, how to display it, and how to filter the WP-Members Members Area links content using the wpmem_member_links filter.

Get Fresh Content Delivered

Join and get free content delivered automatically each time we publish.

Comments

  1. Patricio Silva says

    Dear Chad!
    First thx a lot for a powerful plugin…

    I have a simple question, is it possible to hide the sidebar for non members using the filter hooks in the theme?
    I would like to hide everything including custom menus, etc.

    Thx, /Patricio

    • says

      Yes – you would need to make some edits to your sidebar.php. The framework for that is:

      <?php if( is_user_logged_in() ) {
      	// user is logged in, display content for logged in users
      } else {
      	// user is not logged in, display other content
      } ?>

      Depending on your theme, how your sidebar is constructed, how widgets are utilized, you will need to consider what goes into each of those conditions. But that’s the general idea.

    • says

      hi Patricio, not sure if you solved this or not, but there is a php code widget plugin, which sets it up so that you can put the code above into the widget itself, without having to go into the sidebar.php file. I used it and set it up, and it works great!

      • Patricio Silva says

        Hi Vania, thx for the suggestion. This is a better solution since I’m using WP-members in just one site of many in a MU installation. This way I can just activate that plugin only for the site using WP-members. I will give that a try.

  2. says

    Chad,
    This is a great plugin; I am using it in conjunction with Access Control in order to create an entire Agents Portal myself and all of the real estate agents that work with me at Seven Valley Realty, Inc. I thought I was going to either have to get another plugin to create individual profile pages and play around with the functions.php for a few hours… This definitely shortened my work in this regard. Thanks for the great plugin and for this tutorial on using the native WP functions with WP Members as a filter. Great post!

  3. Wes says

    I’ve been reading about your plugin and it looks like it can work for me. However, I wanted to first find out if it could the following. I am putting together a site where I want my members to have to authenticate to access the members area. Once they’re in the members area, I want them to be able to view other members’ profiles, such as a thumbnail photo of them, their name, phone number and email address. Also, I assume each member can update their own profiles. Can your plugin do this? Thanks so much!

    • says

      Yes, members can update their own profiles. However, the plugin does not show other members’ profiles at this time. You’d need to find another method of doing that.

      • Farid says

        Hi Chad,
        Thanks for your nice plugins.
        You mention that “You’d need to find another method of doing that” – would you please tell me how I can do that? Please details, if possible.
        Regards,
        Farid.

        • says

          Hi Farid –

          The plugin relies on WP’s users and usermeta tables, so everything is standard with WP. So if you wanted to show member profiles, you could either develop some way of delivering that, or there are plugins available. As long as a plugin for displaying user profiles is doing it off the wp_users table, you should find that it would be compatible.

          There is a premium add-on available to members of the support site. That is developed specifically as an add-on to WP-Members. It’s still in early development, with the second version release due out this week.

  4. says

    Hi chad, I saw the last post about your plugin mentioning….”Yes, members can update their own profiles. However, the plugin does not show other members’ profiles at this time. You’d need to find another method of doing that.” (July 18, 2012)…..I need to know if you have Updated this feature yet,. as it is January 7, 2013 now? I really need this feature on my site, as I have a Artists Community, where members display thing on their person Profile Pages. Hope it does, if not, WHEN would this feature be available? Thanks….Blessings.

  5. Don says

    Hi Chad,
    I would like to upload a document to the user’s profile, and make it visible to them to download & view or print. Is this possible?

    Thank you for your support. It is greatly appreciated!

  6. Nathan Sumner says

    Hey Chad,

    This is a great plugin! I’m really hoping I can use it for my current project at work because we want to build a website on WordPress. Ok……This site is a travel site. Basically, the company provides custom trips to people traveling to Latin America. We are going paperless, so I have been tasked with creating a website that will allow us to have the travelers register for an account. Once in their account, we want them to be able to complete a trip application that will store their data in the mysql database. I use both cformsII and Gravity Forms. Is this possible?

    Thanks in advance!

    -Nathan

  7. elena says

    Hi, Chad!
    On my congregation website, if a user wants to edit their profile, it redirects them to the dashboard and media to create a playlist with jw player. Is there a way to remove these options and only have the profile editor available? I searched all over cPanel – could not find where those options are sitting to remove them.
    Thanks.

Join in the discussion