How to Restrict Access to Your WordPress Site for Non-Logged-In Users

Are you looking to restrict access to specific content on your WordPress website and make it available only to logged-in users? Whether you want to create a members-only area, protect premium content, or offer an exclusive experience to registered users, this tutorial will guide you through the process.

In this tutorial, you’ll learn how to restrict access to WordPress content for logged-in users without the need for plugins. We’ll accomplish this by adding a custom code snippet to your WordPress site. This method ensures that non-logged-in users will encounter a message prompting them to log in or register to access the restricted content.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Restrict Access to Logged-In Users

To get started, you’ll need to access your WordPress theme’s functions.php file. This file serves as the central hub for customizing your theme’s functionality.

Open your functions.php file through a code editor or use the built-in Theme Editor within your WordPress dashboard. Add the following code snippet at the end:

if (!function_exists('restrict_access_to_logged_in_users')) {

    add_action('template_redirect', 'restrict_access_to_logged_in_users');

    function restrict_access_to_logged_in_users()
    {
        // Get the current page or post ID
        $page_id = get_queried_object_id();

        // Define the login page ID (replace with your login page's actual ID)
        $login_page_id = 1;

        // Check if the user is not logged in and not on the login page
        if (!is_user_logged_in() && $page_id !== $login_page_id) {
            // Display a message to non-logged-in users
            wp_die('<style>html{background:#000; text-align:center; color:#fff;}</style><h1>Access Restricted</h1><p>Sorry, this content is available only to logged-in users. Please <a style="color:#fff;" href="' . wp_login_url() . '">login</a> or <a style="color:#fff;" href="' . wp_registration_url() . '">register</a> to access this content.</p>');
        }
    }
}
Code language: PHP (php)

Explanation:

  • This code checks whether the function restrict_access_to_logged_in_users exists to prevent conflicts.
  • It uses the template_redirect action hook, which is triggered before any template is displayed, to execute the restrict_access_to_logged_in_users function.
  • Inside the function, it retrieves the current page or post ID.
  • You can define the login page ID by replacing 1 with the actual ID of your login page. This ensures that the message isn’t displayed on the login page itself.
  • If a user is not logged in and is not on the login page, a message is displayed, prompting them to log in or register to access the content.

Save Changes and Test

After adding the code snippet to your functions.php file, save the file, and the changes will take effect immediately on your WordPress site. To test this, log out of your WordPress account or use a different browser or device where you are not logged in. Attempt to access your site, and you should see the message indicating that the content is visible only to logged-in users.

That’s it! You’ve successfully restricted access to specific content on your WordPress website for logged-in users. This method provides a straightforward way to create members-only areas or protect premium content without relying on plugins. By following these steps and adding the provided code snippet to your theme’s functions.php file, you can enhance your website’s user experience and security.

For more advanced access control options and custom criteria, such as restricting access based on custom fields, check out our tutorial on How to Restrict Access to WordPress Pages or Posts for Logged-In Users and Custom Criteria. This comprehensive guide covers various methods to restrict access to specific content on your WordPress site.

Leave your feedback and help us improve 🐶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.

More free knowledge, because why not?

Your thoughts matter, leave a reply 💬

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