How to Redirect WordPress Users to the Login Page on Specific Pages
As a WordPress website owner, you may want to restrict access to certain pages or sections of your site to only logged-in users. This is a common requirement for membership sites, private communities, or any website that needs to protect sensitive information.
One effective way to achieve this is by using the template_redirect
filter in WordPress. This filter allows you to intercept the page loading process and perform custom actions based on the user's login status and the current URL.
In this blog post, we'll explore how to use the template_redirect
filter to redirect non-logged-in users to the WordPress login page on specific pages of your website.
Understanding the template_redirect
Filter
The template_redirect
filter is a WordPress hook that is called after the template file has been chosen, but before the template is loaded. This means you can use it to perform actions based on the current page being requested.
Here's an example of how you can use the template_redirect
filter to redirect non-logged-in users to the login page on specific pages:
function my_page_template_redirect() {
$url_path = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
if( strpos($url_path, '/people/') !== 0 && ! is_user_logged_in() )
{
wp_redirect( wp_login_url() );
die;
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
Let's break down this code:
- The
my_page_template_redirect()
function is defined, which will be called by the template_redirect
hook.
- Inside the function, we use the
$_SERVER['REQUEST_URI']
variable to get the current URL path, and then use the preg_replace()
function to remove any query string parameters.
- We then check if the URL path contains the
/people/
string (this is just an example, you can use any URL pattern you want), and if the user is not logged in (! is_user_logged_in()
).
- If both conditions are true, we use the
wp_redirect()
function to redirect the user to the WordPress login page (wp_login_url()
).
- Finally, we call the
die()
function to stop the script execution and prevent the original page from being loaded.
This code will redirect any non-logged-in user to the login page when they try to access a URL that contains the /people/
string (or any other URL pattern you specify).
Real-World Example: Restricting Access to a "People" Page
Let's consider a real-world example where you have a "People" page on your WordPress website that should only be accessible to logged-in users.
Imagine you have a website for a small business, and the "People" page contains a directory of your employees, their contact information, and other sensitive data. You want to make sure that only your staff (who are registered users on your website) can access this page.
Using the template_redirect
filter, you can easily implement this functionality:
function my_page_template_redirect() {
$url_path = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
if( strpos($url_path, '/people/') !== 0 && ! is_user_logged_in() )
{
wp_redirect( wp_login_url() );
die;
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
In this example, the my_page_template_redirect()
function checks if the current URL path contains the /people/
string and if the user is not logged in. If both conditions are true, the user is redirected to the WordPress login page.
This way, when a non-logged-in user tries to access the "People" page (e.g., https://example.com/people/directory
), they will be redirected to the login page instead of seeing the sensitive employee information.
Implementing the Redirect for Multiple Pages
The example above shows how to redirect non-logged-in users to the login page on a single page. However, you may want to apply this functionality to multiple pages or sections of your website.
To do this, you can modify the strpos()
function call in the my_page_template_redirect()
function to check for multiple URL patterns. For example:
function my_page_template_redirect() {
$url_path = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
if( (strpos($url_path, '/people/') !== 0 || strpos($url_path, '/admin/') !== 0) && ! is_user_logged_in() )
{
wp_redirect( wp_login_url() );
die;
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
In this updated code, the function checks if the URL path contains either the /people/
or /admin/
string. If either of these conditions is true and the user is not logged in, they will be redirected to the login page.
You can continue to add more URL patterns to the strpos()
function call to restrict access to any number of pages or sections on your WordPress website.
Extending the Functionality
The template_redirect
filter is a powerful tool, and you can further extend its functionality to meet your specific requirements. Here are a few ideas:
- Customize the Redirect URL: Instead of redirecting to the default WordPress login page, you can specify a custom login page URL using the
wp_redirect()
function. For example, wp_redirect( 'https://example.com/custom-login-page' );
.
- Display a Custom Message: You can display a custom message to the user before redirecting them to the login page. This can be done by using the
wp_die()
function instead of wp_redirect()
.
- Exclude Specific Pages: If you want to exclude certain pages from the redirect, you can use the
apply_filters()
function to allow other plugins or functions to modify the behavior of the template_redirect
filter.
- Integrate with Other Plugins: You can combine the
template_redirect
filter with other WordPress plugins, such as membership or access control plugins, to create more advanced access control systems.
Conclusion
The template_redirect
filter in WordPress is a powerful tool for controlling access to specific pages or sections of your website. By using this filter, you can easily redirect non-logged-in users to the WordPress login page, ensuring that sensitive content is only accessible to authorized users.
In this blog post, we've covered the basics of using the template_redirect
filter, including a real-world example of restricting access to a "People" page. We've also discussed ways to extend the functionality and integrate it with other WordPress plugins.
If you're looking to improve the security and privacy of your WordPress website, consider implementing the template_redirect
filter to better control access to your content. And remember, Flowpoint.ai can help you identify any technical errors that may be impacting your website's conversion rates and provide recommendations to fix them
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.