Force users to log in before viewing posts in WordPress, the most straightforward method involves using a plugin designed for this purpose.
Using a Plugin:
- Install and Activate the Plugin:
- Navigate to your WordPress admin dashboard.
- Go to Plugins > Add New.
- Search for “Force Login” (or a similar content restriction plugin like ProfilePress).
- Install and activate the chosen plugin.
- Configuration (if necessary):
- Many “Force Login” plugins work automatically upon activation, redirecting all non-logged-in users to the login page.
- If using a more robust content restriction plugin (e.g., ProfilePress), you might need to:
- Create content protection rules under the plugin’s settings.
- Specify which content (all posts, specific categories, or custom post types) requires login.
- Define which user roles are granted access after logging in.
Alternative (for specific pages/posts):
- Set Post/Page Visibility to Private:
- When editing a specific post or page, locate the “Visibility” option in the document settings.
- Change the visibility to “Private.” This will make the content accessible only to logged-in users with appropriate permissions (e.g., administrators, editors).
Note: While code-based solutions are possible (e.g., using auth_redirect() in functions.php), plugins generally offer a more user-friendly and maintainable approach for most WordPress users.
