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] |

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/
What happens in the forums? Does the short code come out as plain text?
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!