Hide WordPress content from people who aren’t logged in

By:

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.

Want to hide WordPress Content from some people?
Raquel Camargo / Foter / CC BY-NC

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:

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:

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:

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:

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:

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…

with

The nested shortcodes above will now work as expected and can hide WordPress content generated by a shortcode!


Leave a Reply

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