Back to Top

Using Shortcodes to Show Members-only Contents

Previous Post:

Using Shortcodes to Show Members-only Contents

While the title and description of this tip can be quite catchy, the methods we are going to use is actually very simple. Before you move on, however, it is recommended that you have some basic understanding of WordPress’s Shortcode API.

Regardless of the type of shortcode you choose (be it self-closing shortcode or enclosing shortcode), the mechanism is very similar. All you have to do is to create a new shortcode1 and then use is_user_logged_in() in its callback function, like so:

// [example]
function example_shortcode_callback($atts)
{
	// If not a registered user, simply return a message
	if (!is_user_logged_in())
		return __('These contents are available to registered users only!');
	// Otherwise, return the hidden contents
}
add_shortcode('example', 'example_shortcode_callback');

Ideally, you should also provide a link to login or register to encourage people to view your hidden contents. Such thing can be done easily with the help of wp_login_url()2 and wp_register()3:

// [example]
function example_shortcode_callback($atts)
{
	// If not a registered user, encourage them to register or login
	if (!is_user_logged_in())
		return sprintf(
			__('These contents are available to registered users only!
			Please <a href="%s">register</a> or <a href="%s">login</a>!'),
			wp_register('', '', false), wp_login_url()
		);
	// Otherwise, return the hidden contents
}
add_shortcode('example', 'example_shortcode_callback');

Please note that is_user_logged_in() can not be used to give access to hidden contents to certain kinds of members, such as members of Author role, or members with ‘view_download_links’ capability (just for example).

If such behaviour is what you’re after, WordPress’s Roles and Capabilities, current_user_can(), and add_role() codex pages can be a good read.

Now for some real life examples, I am going to create a self-closing shortcode named ‘hide_links’ and it will optionally take some download links from the database and then show it to registered members only. Self-closing shortcodes are good for this purpose because we do not need any actual text at all.

// [hide_links link="link-to-file"]
function hide_download_links($atts)
{
	global $post;
	// If not a registered user, simply return a message
	if (!is_user_logged_in())
		return __('Download Links are available to registered users only!');
	// Otherwise, parse the shortcode
	extract(shortcode_atts(array(
		'link' => '',
	), $atts));
	// Take a link from attribute?
	if (!empty($link))
		return '<a href="' . $link . '">' . __('Download Link') . '</a>' . "\n";
	// No link found, get from database instead
	else if (!empty($post->ID))
	{
		$count = 0;
		$output = '';
		$links = get_post_meta($post->ID, 'DownloadLinks');
		if ($links && is_array($links))
		foreach ($links as $link)
		{
			$count++;
			$output .= '<a href="' . $link . '">' . sprintf(__('Part %d'), $count) . '</a>' . "<br />\n";
		}
		return $output;
	}
}
add_shortcode('hide_links', 'hide_download_links');

Now if you use [hide_links link="http://example.com/members-only/download/file.ext"] in your post, the download link will appear like this:

// to guests
Download Links are available to registered users only!
// to members
Download Link

If you use the DownloadLinks meta key to store download links for a particular post/page, you can then use [hide_links] to show all associated download links. The output will be similar:

// to guests
Download Links are available to registered users only!
// to members
Part 1
Part 2
Part 3
etc.

Very simple, right? You can use the same technique to hide anything you want, which is of course only limited by your own imagination!

References

  1. http://codex.wordpress.org/Function_Reference/add_short ... _shortcode []
  2. http://codex.wordpress.org/Function_Reference/wp_login_ ... _login_url []
  3. http://codex.wordpress.org/Function_Reference/wp_regist ... p_register []

Take Social Sharing to
the Next Level with Monarch!

Take Social Sharing to the Next Level with Monarch!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

3 Opinions for Using Shortcodes to Show Members-only Contents

  1. User's Gravatar
    1
    Moralize April 12, 2011 at 6:40 pm – Permalink

    A very nice tip, thanks :D!

  2. User's Gravatar
    2
    Robert Steers July 27, 2011 at 11:54 am – Permalink

    I love using shortcodes, but I just can’t get one to work. I am trying to return a string, but it is only returning the first part of it.

    Anyway, I came across your post looking for a solution, and thought you did a great job of breaking it down!

  3. User's Gravatar
    3

    Thank you. An abundance of info!

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.




  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: