Aug 08, 2011 Defining Content For Specific User Roles (Wordpress)

Earlier today I was working on a clients WordPress install and the client’s site has a user registration feature, the rub was the client did not want normal readers to view a certain selection of links.

My first thoughts were "there will be a plugin for that" and after researching a number of likely candidates they all seemed to be too complex or covered far more than my client would actually need since in this occasion we are only hiding a few links from a certain type of visitor.

With WordPress being PHP driven the obvious choice was to use a few "IF" statements to get the desired result, I visited The WordPress Codex and searched for the entry on Roles and Capabilities and set to work on a suitable solution to my client’s request.

WordPress fortunately has a limited number of roles on the typical install: Super Admin, Administrator, Editor, Author, Contributor and Subscriber, below is the basic statement I started with, each role is defined and each have a section where content can be added, I achieved this by using the current_user_can() function. (In my examples I covered admin, editor and author roles but you can use the other roles too if you wish.)

The following snippet should be placed where you would like the content to display, in this example I placed the snippet inside of "sidebar.php."

if( current_user_can('administrator') ) {
    // content for admin eyes only here
}

if( current_user_can('editor') ) {
    // content for editors eyes only here
}

if( current_user_can('author') ) {
    // content for editors eyes only here
}

Once I had jotted that down the next step was to test it out and ensure the right users could see the right content, so I added a little content to each to ensure my snippet was fully functional:

if( current_user_can('administrator') ) {
    // content for admin eyes only here
    echo'<a href="#">admin link</a>';
}

if( current_user_can('editor') ) {
    // content for editors eyes only here
    echo'<a href="#">editor link</a>';
}

if( current_user_can('author') ) {
    // content for editors eyes only here
    echo'<a href="#">author link</a>';
}

I tested the above code snippet and the correct links could only be viewed by the correct user role as I would have hoped, my client had several links that were relevant to his WordPress install, but an example utilisation of this in action would be for the admin meta links for example:

if( current_user_can('administrator') ) {
    // content for admin eyes only here
    echo'<a href="#">Admin Page</a>';
    echo'<a href="YOURSITE.com/wp-admin">WP Admin</a>';
    echo'<a href="YOURSITE.com/wp-login.php?action=logout">Log Out</a>';
}

if( current_user_can('editor') ) {
    // content for editors eyes only here
    echo'<a href="#">Editor Page</a>';
    echo'<a href="YOURSITE.com/wp-admin">WP Admin</a>';
    echo'<a href="YOURSITE.com/wp-login.php?action=logout">Log Out</a>';
}

if( current_user_can('author') ) {
    // content for editors eyes only here
    echo'<a href="YOURSITE.com/wp-admin">WP Admin</a>';
    echo'<a href="YOURSITE.com/wp-login.php?action=logout">Log Out</a>';
}

I hope you find this useful, it was something that I did some work on today and hopefully it might save a little time in any of your future projects or maybe you will be able to use it in one of your current projects? If you feel there is a better way to achieve this or you feel I have made a mistake please do comment below!

Comments
Post a comment