WordPress Shortcodes for Users and Non Users

One of the plugins I wrote for the DC WordPress group’s meeting on Users / Roles / Capabilities demonstrates how to create shortcode that makes some content only visable by users with a certain capability and other content only visable to non logged in viewers.  You can download Shortcodes for Discrimination (along with the other three ) from google code.  Beyond the jump, I go over the code and an explanation of how it works.

The plugin starts with the usual plugin headers and then includes some license information. After that, we start to get to the good stuff.

[php]

class jorbin_user_shortcodes{

/**
* Add our Init action, which is where the real work begins
*/
function __construct(){
add_action( ‘init’, array( $this , ‘init’) );
}
[/php]

Our first function is the class constructor. This this will get fired before the majority of WordPress is ready to act, all I do is add an action to the init hook which is where the real additions take place.

[php] /**
* Starter up
*
*/
function init(){
add_shortcode(‘user’, array( $this, ‘logged_in_shortcode’) );
add_shortcode(‘notuser’, array( $this, ‘not_logged_in_shortcode’) );
}[/php]

The plugins init method (which I hooked to init above) adds two shortcodes using the Shortcode API. The first argument is what our shortcode will be, and the second is our callback. Since it’s happening in a class, our callback is an array. Let’s take a look at the first callback that controls the [user] shortcode.
[php]/**
* Shortcode Callback to return shortcode contents only for users with a certain capability
*
*/
function logged_in_shortcode($atts, $content){
extract( shortcode_atts(
array(‘capability’ => ‘read’,)
, $atts));

if ( current_user_can ($capability) )
return $content;
else
return ”;

}
[/php]

The first part of the function extracts our shortcode attributes so if I did a shortcode like [shortcode foo=’bar’], then $foo would be equal to bar. In this case, capability is the attribute and it has the default setting of ‘read’ if no attribute is specified.

After that, we do a check of current_user_can with the capability specified. If it returns true, the content (What is between [user] and [/user] ) is included in the post. Otherwise, nothing is included in that spot.

[php]

function not_logged_in_shortcode($atts, $content){

if ( is_user_logged_in() )
return ”;
else
return $content;

}
[/php]
The second callback does something similiar, only this time we check is_user_logged_in which returns true when the person viewing the page is logged in. if it’s true, the content disappears. If it’s false, it’s visible. Easy. As. Pie.

All we have to do to finish things up is call
[php]$jorbin_user_shortcodes = new jorbin_user_shortcodes();[/php] and our shortcode is active. Download the plugin from google code and have fun!

2 thoughts on “WordPress Shortcodes for Users and Non Users”

  1. Aaron,

    My name is Jason and I have stumbled across your site and glad I did. I see you mentioned about a DC WordPress group and I would like to find out how I could join the group. I live in N.Va and I’m new to WordPress and trying to learn all I can. I look forward to hearing from you, thanks for your time.

    Jason

Leave a Reply

Your email address will not be published. Required fields are marked *