video-poster

Being able to restrict certain content to logged in members only is a very useful function, especially for membership based websites, whether free or premium. This quick tutorial shows you the basics of how to write a short code function that will allow you to limit blocks of content to logged in users only.

Check out the video for an explanation of what is going on, or skip straight to the code.

The code needed to do this is pretty minimal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function pippin_logged_in_user_shortcode($atts, $content = null) {
	extract( shortcode_atts( array(
			'message' => ''
		), $atts )
	);
 
	if(is_user_logged_in()) {
		return $content;
	} else {
		return '<div class="alert_red" style="color: red;">' . $message . '</div>';
	}
 
}
add_shortcode('logged_in', 'pippin_logged_in_user_shortcode');

With this function active, you will now be able to do this:

1
[logged_in message="You must be logged in to view this content"]This is text that can only be viewed when logged in[/logged_in]
  1. John Shipka

    this is a great post and very helpful!

    I am working with another plugin for my forum called bbpress. I can get your plugin to work great for my pages but not for any forums. Any suggestions? Any and all help is appreciated. Thanks!

    here is the link i’m working on.
    http://appraiserpanel.com/forum/

    • Pippin

      What happens in the forums? Does the short code come out as plain text?

    • John Shipka

      Not plain text. The page content was visible. I went in another direction on the site but plan to use your plug in in the future. Very nice. Thanks!

  2. Jeff Campagna

    This was immensely helpful! Thanks, Pippin!

  3. Jeff Campagna

    Hey Pippin. I seem to be able to use this to hide buttons from logged in users with [ not_logged_in ][ button ][ /button ][ /not_logged_in ] in my footer.

    But I can’t seem to hide buttons from NOT logged in users with [ logged_in ][ button ][ /button ][ /logged_in ] in a post.

    Any idea why? I’ve got your function in my php.

    • Pippin

      You can create a [not_logged_in] short code version and then use that.

  4. M.

    Hey Pippin. Thanks again for the great plugin.

    We’re trying to restrict a button link to paid members only whereby a popup will inform non-logged in and free users to start a free trial.

    Using javascript we have successfully created the popup message and blocked the link for ALL users; however we want this javascript code only to work for non-logged in and free members.

    Any ideas?

    Thanks

  5. Moh Green

    Hey Pippin omit that previous comment as we figured out a solution! It is so simple, the [is_not_paid] shortcode works perfectly.

    Thanks. :)

    • Pippin

      Glad to hear it!

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