Whether you are launching a course, publishing premium content, creating a private blog, or a client-only portal, you want your website to be accessed only by members of your website.
Forcing visitors to log in before viewing a page is a great way to ensure that only authorized members are allowed to access your website.
WordPress does not have a native feature to force visitors to log in but fortunately, there is a plugin to help you do just that. We will show you how to use the plugin to implement force logins.
If the plugin fails to work on your website, then try using a code snippet. We will show you how to manually add a code snippet to your WordPress website to help implement force logins prior to accessing your site.
Let’s dive into the tutorial.
How to Force Visitors to Login Before Entering WordPress Website
There are 2 ways to force visitors to log in before accessing any of your pages. You can
- Install a plugin called Force Login or
- Manually add a code snippet to your site’s theme
In this tutorial, we will walk you through both methods. Just make sure you take a backup of your website before diving into any of the methods shown below.
Installing a new plugin or adding code snippets are known to cause websites to break. If that happens, the backup will be your safety net. You can use the backup copy to get your website up and running in no time. Get a backup of your entire website before you proceed with the tutorial.
Method 1: Force Login in WordPress Using a Plugin
There aren't many dedicated plugins to implement force login and the only working plugin available at the time of writing this article is Force Login By Kevin Vess. It’s a free plugin and using it is super simple.
We will show you the exact steps you need to take to use the plugin on your site.
Open your WordPress dashboard and from the left-hand menu, go to Plugins > Add New.
Next, look for Force Login in the search bar. When the plugin appears hit the Install Now button followed by the Activate button.
Another way to install the plugin is by downloading the Force Login plugin first and then going to Plugins > Add New. In the Add Plugins page, choose the Upload Plugins option.
Then select the Choose File button and upload the plugin you downloaded in the previous step. Hit the Install Now button to initiate the installation.
As soon as you select the Install Now button, you will be taken to a different page and asked to activate the plugin. All you need to do is hit the Activate Plugin button.
The plugin is now activated on your WordPress website and the best part is you don’t need to take any extra steps to implement force logins for visitors. The plugin has automatically implemented force login.
Try accessing your website via incognito. You will find yourself being redirected to the login page.
Some of you might encounter compatibility issues with the Force Login plugin. In that case, please proceed with the following manual method.
Method 2: Force Login WordPress Using Code Snippet
To force login using a code snippet, you will need to add a code to the active theme of your website. But since modifying the theme is risky and can result in a broken website, we recommend using a plugin to add a code to the site.
Another solution is the installation of a Child Theme in that case you will write only the child theme functions.php file and not the original one. In this case, we will describe the process with the usage of the Code Snippets plugin.
There are plenty of code insertion plugins to choose from. The most popular and trustworthy plugins are Code Snippets and WPCode.
In this tutorial, we are using Code Snippets but if you choose to use WPCode, the following steps should still give you an idea of how to use the plugin.
Now, let’s get started!
Download Code Snippets then upload, install and activate the plugin on your WordPress website. Or simply open your WordPress dashboard, go to Plugins > Add New > Upload Plugins, and proceed to upload, install, and activate the plugin.
After activation, you will see a Snippets option appear on the left-hand menu. Go over to that option and choose Add New. You will be taken to a page, where you can add a code snippet to implement forced login.
Add a title and insert the following code snippet in the Functions tab:
function v_getUrl() {
$url = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http';
$url .= '://' . $_SERVER['SERVER_NAME'];
$url .= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT'];
$url .= $_SERVER['REQUEST_URI'];
return $url;
}
function v_forcelogin() {
if( !is_user_logged_in() ) {
$url = v_getUrl();
$whitelist = apply_filters('v_forcelogin_whitelist', array());
$redirect_url = apply_filters('v_forcelogin_redirect', $url);
if( preg_replace('/?.*/', '', $url) != preg_replace('/?.*/', '', wp_login_url()) && !in_array($url, $whitelist) ) {
wp_safe_redirect( wp_login_url( $redirect_url ), 302 ); exit();
}
}
}
add_action('init', 'v_forcelogin');
After inserting the code, scroll down to the end of the editor and make sure the “Run snippet everywhere” option, located underneath the editor, is selected.
Next, hit the Activate button that appears on the top right side of the screen.
That’s it, folks! You have implemented the force login function on your WordPress website.
We recommend testing your site to ensure that the function works on your site. Open incognito and go to your home page. Your site should redirect you to the login page.
You can also read more about:
Add SSL and Move From HTTP to HTTPS
How to add “Continue Reading” button on my blog posts?
Conclusion
Forcing your visitors to log in to access the content of your website is a great way to ensure that only authorized members can enter your site.
Since WordPress does not have a native feature to allow forced login, you need a third-party tool.
You can use a plugin called Forced Login or manually insert a code snippet into your active theme to add a forced login function to your website. We strongly recommend against making any modification to the theme and suggest using a code insertion plugin like Code Snippets or WPCode to add the code snippet to the site.
As you can see, implementing forced login to your WordPress website is not a complicated procedure but you are certainly making changes to the site and that’s risky. Make sure you take a backup of your entire website before proceeding with the steps.
If you have any questions about forcing login before visitors access WordPress website, let us know in the comment section below.
Comments