Add Snippet To Project
WordPress is a versatile and popular platform that allows users to create and customize websites with ease. However, there may be times when you want to make your WordPress blog private and limit access to only certain users. This could be for a variety of reasons, such as creating a private blog for your family or team members, or simply keeping your personal blog hidden from public view. In this article, we’ll show you how to make your WordPress blog private and control who has access to it.
function wpturbo_make_blog_private() {
if ( ! is_user_logged_in() ) {
auth_redirect();
}
}
add_action( 'template_redirect', 'wpturbo_make_blog_private' );
The purpose of this code snippet is to make a WordPress blog private, meaning that only logged-in users can access its content.
The wpturbo_make_blog_private()
function checks if the user is currently logged in. If they are not, the function will redirect them to the login page using the auth_redirect()
function.
This function gets called when the template_redirect
action is fired, which occurs after the header template file is loaded but before WordPress outputs any HTML. This ensures that the redirect happens before any content is displayed, which means that the user will not be able to view any of the blog’s pages or posts without logging in first.
To use this code on your WordPress site, you can simply copy the code snippet and paste it into your theme’s functions.php
file. Once you have done this, your blog will require users to log in before they can access any content.