How To Force Users To Login in WordPress
Suppose you want to create a WordPress website but don’t want it to be available for everyone. Maybe you want to build a member-only platform where some members or subscribers of your site can log in to it, Or maybe your blog has unique content that you want to make available for registered/paid users only. Then you have to find how to force users to login to WordPress.
When you force a login to WordPress, you tell the users that you can only see the content of that particular site if they login to the website. However, A force login for a user does not mean that they have admin or editor access to your website. It only means that users log in as a member-only to get the content.
Few Reasons Why You Require to log in to a Page/Website
There can be many reasons you require to log in to a page or website. Some explanations are below.
- Maybe you have a private blog or website you don’t want to share with everyone. Suppose you are working on a project that is not ready, and you want to restrict it. Therefore, Only specific people could log in to it.
- If you own a membership website and want to allow a particular member to a specific page, they must log in to view them.
- If you offer paid online courses or content, you have to protect the site from non-users. Users have to become paid members of that site and create their accounts to log in.
To force a login in WordPress, you can either use some code or use a plugin. Also, you can use some different PHP codes for specific situations.
Below are the methods explained.
#Methods to force users to login WordPress
1.By using The Force Login plugin
First, we are using the plugin method. This plugin is straightforward to use. It helps you to hide your WordPress website from public viewing. Visitors need to log in to visit your site.
You can Install and activate the plugin by using our guide or by simply following the below steps:
Go to Plugin >> Add new >> search for Force Login plugin.
You are done! Good to go. You don’t have to do further settings as this plugin is automatically turned on.
If someone tries to go to your website or any URL on your site, they will directly reach the login page for your website. Here, they can log in with their details to get the content.
2.By using some coding tricks
We can use some codes to force Login in different situations. You have to access your function.php file and add the code.
The codes are for the relevant situation. You can use whatever you want.
- Go to Appearance >> Theme Editor
- click on theme functions (function.php) >> use any code >> save
Redirect Login to a specific URL
You can redirect your users to a particular URL by using the following code.
[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]/** * Set the URL to redirect to on Login. * * @param string $url The visited URL. * @return string The URL to redirect to on Login. Must be absolute. */ function my_forcelogin_redirect( $url ) { return home_url( ‘/mypage/’ ); } add_filter( ‘v_forcelogin_redirect’, ‘my_forcelogin_redirect’ );[/ht_message]
#Adding Exceptions for Certain Pages/post
You can bypass force login on any condition. You can use WordPress conditional tags also.
Bypass a Force Login
You can use the following code for a Bypass login.
[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]/** * Bypass Force Login to allow for exceptions. * * @param bool $bypass Whether to disable Force Login. Default false. * @return bool */ function my_forcelogin_bypass( $bypass ) { if ( is_single() ) { $bypass = true; } return $bypass; } add_filter( ‘v_forcelogin_bypass’, ‘my_forcelogin_bypass’ );[/ht_message]
URLs of Whitelist
You can use the following code for the URLs of Whitelist.
[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]/** * Filter Force Login to allow exceptions for specific URLs. * * @param array $whitelist An array of URLs. Must be absolute. * @return array */ function my_forcelogin_whitelist( $whitelist ) { $whitelist[] = home_url( ‘/mypage/’ ); $whitelist[] = home_url( ‘/2015/03/post-title/’ ); return $whitelist; } add_filter( ‘v_forcelogin_whitelist’, ‘my_forcelogin_whitelist’ );[/ht_message]
Hiding the ‘Back to Sitename’ Link
If you want to hide the Back to Sitename link, you can use the following code. The Back to site name link is always present on the WordPress login page.
[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]// Hide the ‘Back to {sitename}’ link on the login screen. function my_forcelogin_hide_backtoblog() { echo ‘<style type=”text/css”>#backtoblog{display:none;}</style>’; } add_action( ‘login_enqueue_scripts’, ‘my_forcelogin_hide_backtoblog’ );[/ht_message]
Wrapping up:
If you want to hide your WordPress website because you want to keep it private or allow for registered members, you can force Login before visitors access your content. You can use an easy-to-use plugin or code options like bypass force login and whitelist URLs.
We hope this helps understand how to force users to login to WordPress.