Adding Visibility Options

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