WordPress has a built in feature which allows you to set the visibility of a page. If you set a page to “private” the page will only be visible to people logged onto your website.
By default if a non-logged-in user visits the page URL they will be directed to the 404 page. In some cases this is fine but in some cases this isn’t very helpful. For example if you wanted to create a private page only for your clients. Say you wanted to post private information that you want them to see but don’t want the public to view. Chances are they would be confused when they see a 404 page after visiting the link you send them. They would most likely be even more confused when you send them a link to the WordPress login page and then after they log in they are at the dashboard, etc. What would be ideal would be if non-logged-in users get directed automatically to the WordPress login screen when clicking on the private page URL and then redirected back to the page after a successful login. We can do this easy enough with a couple steps.
Install Private Content Login Redirect WordPress Plugin
First install this plugin on your WordPress website. There are no configuration options, just install it and activate it.
Now when somebody clicks on the link to your private WordPress page they will be automatically redirected to your WordPress login screen. After logging in they will be redirected back to the page they were originally trying to visit, and now they can view the content on the page.
There is still one issue with this setup though. By default only editor and administrator level users are allowed to read/view private WordPress pages. This doesn’t work in our example where we are making unique users for each of our clients, we obviously don’t want them to have editor or administrator roles to our website, we want them to have the lowest level “subscriber” role.
Allow Subscriber User Roles to View WordPress Private Pages and Posts
Add the following code to your functions.php theme file. This allows subscriber level users to view the page as well.
$subRole = get_role( 'subscriber' ); $subRole->add_cap( 'read_private_posts' ); $subRole->add_cap( 'read_private_pages' );