Adding Custom Login/Logout Redirects to Your Plugin

Posted on 21st June 2023

Adding Custom Login/Logout Redirects to Your Plugin
If you’re building a WordPress plugin that requires a custom login page or custom logout redirect, you can use the wp_login_url() and wp_logout_url() functions to get the URLs you need.

In this article, we’ll show you how to use these functions to add custom login and logout redirects to your plugin.

We’ll assume that you’re already familiar with the basics of plugin development. If you’re not, check out our guide on How to Create a WordPress Plugin.

Getting Started
Let’s start by creating a new plugin. For this example, we’ll call our plugin Custom Login/Logout Redirects.

Create a new directory for your plugin and add a file named custom-login-logout-redirects.php to it. Then, add the following code to the top of the file:

‘https://example.com/login-page/’,
) );
echo $login_url; // https://example.com/wp-login.php?referrer=https%3A%2F%2Fexample.com%2Flogin-page%2F&redirect_to=https%3A%2F%2Fexample.com%2Fmy-account%2F
If you want to set the remembered user login cookie, you can use therememberme parameter. This parameter takes a boolean value of true or false. If it’s set to true, the cookie will be set. If it’s set to false, the cookie will not be set.

$login_url = wp_login_url( ‘https://example.com/my-account/’, array(
‘rememberme’ => true,
) );
echo $login_url; // https://example.com/wp-login.php?rememberme=true&redirect_to=https%3A%2F%2Fexample.com%2Fmy-account%2F
Using the wp_logout_url() Function
The wp_logout_url() function returns the URL for the WordPress logout page.

If you just need the URL, you can use the function like this:

$logout_url = wp_logout_url();
echo $logout_url; // https://example.com/wp-login.php?action=logout
If you want to redirect the user to a specific URL after they log out, you can use the redirect_to parameter. This parameter takes a URL as a string.

$logout_url = wp_logout_url( ‘https://example.com/’ );
echo $logout_url; // https://example.com/wp-login.php?action=logout&redirect_to=https%3A%2F%2Fexample.com%2F
If you want to set the URL of the page the user is coming from, you can use the Referrer parameter. This parameter also takes a URL as a string.

$logout_url = wp_logout_url( ‘https://example.com/’, array(
‘referrer’ => ‘https://example.com/my-account/’,
) );
echo $logout_url; // https://example.com/wp-login.php?referrer=https%3A%2F%2Fexample.com%2Fmy-account%2F&action=logout&redirect_to=https%3A%2F%2Fexample.com%2F
If you want to set the login cookie, you can use the login parameter. This parameter takes a URL as a string.

$logout_url = wp_logout_url( ‘https://example.com/’, array(
‘login’ => ‘https://example.com/login-page/’,
) );
echo $logout_url; // https://example.com/wp-login.php?login=https%3A%2F%2Fexample.com%2Flogin-page%2F&action=logout&redirect_to=https%3A%2F%2Fexample.com%2F
Adding a Custom Login Redirect
Now that we’ve seen how the wp_login_url() function works, let’s use it to add a custom login redirect to our plugin.

First, we need to register a new query variable. We’ll use this query variable to store the URL of the page we want to redirect the user to.

add_filter( ‘query_vars’, ‘custom_login_logout_redirects_query_vars’, 10, 1 );
function custom_login_logout_redirects_query_vars( $query_vars ) {
$query_vars[] = ‘redirect_to_custom’;
return $query_vars;
}
Next, we need to add a new rewrite rule. This rewrite rule will catch the redirect_to_custom query variable and redirect the user to the URL stored in that variable.

add_action( ‘init’, ‘custom_login_logout_redirects_rewrites’ );
function custom_login_logout_redirects_rewrites() {
add_rewrite_rule(
‘^login/?$’,
‘index.php?redirect_to_custom=$matches[1]’,
‘top’
);
}
Now that we’ve registered our query variable and added our rewrite rule, we can use the wp_login_url() function to redirect the user to the URL stored in the redirect_to_custom query variable.

add_filter( ‘login_redirect’, ‘custom_login_logout_redirects_login_redirect’, 10, 3 );
function custom_login_logout_redirects_login_redirect( $redirect_to, $requested_redirect_to, $user ) {
if ( isset( $_REQUEST[‘redirect_to_custom’] ) ) {
$redirect_to = wp_login_url( $_REQUEST[‘redirect_to_custom’] );
}
return $redirect_to;
}
Now, when a user logs in, they’ll be redirected to the URL stored in the redirect_to_custom query variable.

Adding a Custom Logout Redirect
Now let’s take a look at how we can use the wp_logout_url() function to add a custom logout redirect to our plugin.

First, we need to register a new query variable. We’ll use this query variable to store the URL of the page we want to redirect the user to.

add_filter( ‘query_vars’, ‘custom_login_logout_redirects_query_vars’, 10, 1 );
function custom_login_logout_redirects_query_vars( $query_vars ) {
$query_vars[] = ‘red