Hide WordPress content from people who aren’t logged in
This article describes how to hide WordPress content from users who aren’t logged in to your site. This is how to hide things ‘in-page’ so your users can have exclusive content.
Hide WordPress content for users that are not logged in
Many people want to focus on hiding content from this group of users, but I want to start by showing them content. Most traffic to your site will likely be through non-logged in users, so make sure you give this group of people something and not hide wordpress content from everybody!
Open your theme’s functions.php file in your favorite text editor. Add this PHP code:
1 2 3 4 5 6 |
add_shortcode( 'visitor', 'visitor_check_shortcode' ); function visitor_check_shortcode( $atts, $content = null ) { if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() ) return $content; return ''; } |
Anytime you write a post/page, add this to only show content to users that are not logged in:
[visitor]Some content for the people just browsing your site.
[/visitor]You should also note that this content will be added to your feeds. The next two techniques will hide content from feed readers and others on your site.
Showing content to logged-in users
Now, you’ll see how to show content only to users that are logged into your site. This will be hidden from all other users and not shown in your feeds.
Add this code to your theme’s functions.php file:
1 2 3 4 5 6 |
add_shortcode( 'member', 'member_check_shortcode' ); function member_check_shortcode( $atts, $content = null ) { if ( is_user_logged_in() && !is_null( $content ) && !is_feed() ) return $content; return ''; } |
Then, all you must do is add some content in between your [member] tags when writing a post/page like so:
[member]This is members-only content.
[/member]Showing content depending on user’s capability
This bit of code is my favorite because it allows me to check for users based on their capabilities. You can use something like the Role Manager plugin to create custom capabilities or just use the default WordPress capabilities to check against.
Add this code to your theme’s functions.php file:
1 2 3 4 5 6 7 |
add_shortcode( 'access', 'access_check_shortcode' ); function access_check_shortcode( $attr, $content = null ) { extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) ); if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() ) return $content; return ''; } |
Now, we’re going to show content only for someone that has the capabilitiy of switch_themes (an administrator in the default WordPress setup):
[access capability=”switch_themes”]The currently logged-in user is a god on this site.
[/access]The default capability is read in the code above, so adding this will give access to the content to any logged-in user that can read (this is likely everyone):
[access]You can read, right?
[/access]How to show content if user doesn’t meet requirements
These shortcodes are great if you just want to hide WordPress content. But, if you want to show a message for people that you’re hiding content from, you’ll need to make a small change.
In each of the code snippets above, the line just before the last is:
1 |
return ''; |
That means nothing will be shown if the user doesn’t meet the requirements defined by the shortcode. In order to leave them a message, change it to this:
1 |
return 'Sorry, but you cannot access this content without...'; |
But, nested shortcodes don’t work?
To make this work, replace this in any shortcode functions you’ve added into functions.php
So that’s it – now you know how to hide wordpress content from prying eyes…
1 |
return $content; |
with
1 |
return '' .do_shortcode($content) .''; |
The nested shortcodes above will now work as expected and can hide WordPress content generated by a shortcode!
Leave a Reply