Adding Visibility Options

I’ve searched for plugins but while some do halfway accomplish the task they are normally very complicated.

I’d like to add option to the visibility menu when creating a page (and post, but page is more urgent).

visibility menu wordpress

These options need to be: “Only visible to logged user” and “Only visible to guest (i.e. not logged in”

Does any one know of plugin or and point me in the right direction to write my own?

EDIT: A use case for this is; only show the registration and login pages to ‘guests’. A logged-in user doesn’t need to see these pages.

2 Answers
2

I’ve actually done this before with a custom page template rather than with the visibility options … because both logged in and not logged in users ended up going to the same page. Here’s some pseudo-code (i.e. do not actually use the code, but it will give you an idea)

$logged_in = is_user_logged_in();

switch($logged_in) {
    case true:
        // Do stuff for logged in users
        break;
    case false:
    default:
        // Do stuff for not logged in users
        break;
}

Basically, you check to see if the user is logged in (using a combination of get_currentuserdata() and some checks on their ID). If the user is logged in, you display one block of content. If the user is not logged in, you display another block of content.

I definitely think having a visibility option would be a more elegant solution, but then you’d need two different pages for each kind of user. Would a logged in user see a 404 error page if they went to the wrong site? Or be directed elsewhere? With two separate pages, this is a situation you’ll have to handle gracefully.

Leave a Comment