Building a Custom Login Redirect Plugin for WordPress

Posted on 20th June 2023

As a WordPress plugin developer, you may be asked to create a plugin that redirects users to a specific page after they login. This is a relatively simple task, but there are a few things to keep in mind when building a custom login redirect plugin for WordPress. In this article, we’ll cover how to build a custom login redirect plugin for WordPress and the considerations you need to take into account.

Creating the Plugin

The first step is to create the plugin. We’ll start by creating a new directory in the WordPress plugin directory (wp-content/plugins) and we’ll call it custom-login-redirect. In this directory, we’ll create a file called custom-login-redirect.php.

The plugin will need to do two things:

The plugin will need to register a new setting with WordPress so that the administrator can specify the page to redirect to after a user logs in.
The plugin will need to hook into the WordPress login process and redirect the user to the specified page after they login.

Let’s start by adding the code to register the new setting. We’ll add the following code to the top of our custom-login-redirect.php file:

‘string’,
‘show_in_rest’ => false,
‘default’ => ”,
)
);
}
add_action( ‘admin_init’, ‘custom_login_redirect_register_setting’ );

The code above registers a new setting with WordPress. The setting is registered in the general settings section and it’s called custom_login_redirect_setting. The setting is a string type and it’s not shown in the WordPress REST API.

Next, we need to add the code that will redirect the user after they login. We’ll add the following code to our plugin:

<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Redirect the user after they login
*/
function custom_login_redirect_login_redirect( $redirect_to, $request, $user ) {

// Get the administrator setting
$redirect_page_id = get_option( 'custom_login_redirect_setting' );

// If the setting is not empty, redirect the user
if ( ! empty( $redirect_page_id ) ) {
$redirect_to = get_permalink( $redirect_page_id );
}

return $redirect_to;
}
add_filter( 'login_redirect', 'custom_login_redirect_login_redirect', 10, 3 );

The code above hooks into the WordPress login process and redirects the user to the page specified in the administrator setting.

Considerations

There are a few considerations to keep in mind when building a custom login redirect plugin for WordPress.

Page Permissions

The first consideration is page permissions. By default, WordPress does not allow non-logged in users to view pages. This means that if you redirect a user to a page that they do not have permission to view, they will be redirected to the WordPress login page.

To avoid this, you need to make sure that the page you’re redirecting the user to is accessible to non-logged in users. You can do this by changing the page permissions in the WordPress admin (Settings > Reading > For each page, specify whether it should be visible to everyone, or only logged in users).

Redirect URL

The second consideration is the redirect URL. By default, WordPress will redirect the user to the home page after they login. This is usually the desired behavior, but in some cases, you may want to redirect the user to a specific page.

To do this, you need to add the following code to your plugin:

<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Redirect the user after they login
*/
function custom_login_redirect_login_redirect( $redirect_to, $request, $user ) {

// Get the administrator setting
$redirect_page_id = get_option( 'custom_login_redirect_setting' );

// If the setting is not empty, redirect the user
if ( ! empty( $redirect_page_id ) ) {
$redirect_to = get_permalink( $redirect_page_id );
}

return $redirect_to;
}
add_filter( 'login_redirect', 'custom_login_redirect_login_redirect', 10, 3 );

The code above hooks into the WordPress login process and redirects the user to the page specified in the administrator setting.

Logout Redirect

The third consideration is the logout redirect. By default, WordPress will redirect the user to the home page after they logout. In some cases, you may want to redirect the user to a specific page.

To do this, you need to add the following code to your plugin:

<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Redirect the user after they login
*/
function custom_login_redirect_login_redirect( $redirect_to, $request, $user ) {

// Get the administrator setting
$redirect_page_id = get_option( 'custom_login_redirect_setting' );

// If the setting is not empty, redirect the user
if ( ! empty( $redirect_page_id ) ) {
$redirect_to = get_permalink( $redirect_page_id );
}

return $redirect_to;
}
add_filter( 'login_redirect', 'custom_login_redirect_login_redirect', 10, 3 );

The code above hooks into the WordPress login process and redirects the user to the page specified in the administrator setting.

Conclusion

In this article, we’ve covered how to build a custom login redirect plugin for WordPress. We’ve also covered the considerations you need to take into account when building such a plugin.

Creating a custom login redirect plugin for WordPress is a great way to customize the user experience on your website. By default, WordPress will redirect users to the home page after they login, but you can use a plugin to change this behavior.

There are a few login redirect plugins available, but they may not offer the exact functionality you need. Creating your own plugin is the best way to ensure that the login redirect works exactly as you want it to.

In this tutorial, we’ll show you how to create a custom login redirect plugin for WordPress. We’ll cover the following topics:

Creating the plugin file

Adding a settings page

Saving the plugin settings

Redirecting users after login

Creating the Plugin File

The first thing you need to do is create a new file in your WordPress plugins directory. You can name the file anything you want, but we recommend something like “login-redirect.php”.

Next, you’ll need to add the following code to the top of your plugin file:

<?php
/*
Plugin Name: Login Redirect
Plugin URI: https://example.com/login-redirect
Description: A plugin to customize the WordPress login redirect.
Version: 1.0
Author: John Doe
Author URI: https://example.com
License: GPLv2 or later
Text Domain: login-redirect
*/

// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

if ( !class_exists( 'Login_Redirect' ) ) {

class Login_Redirect {

/**
* Constructor
*/
public function __construct() {

// Add the settings page
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );

// Add the plugin settings
add_action( 'admin_init', array( $this, 'register_settings' ) );

// Handle the login redirect
add_filter( 'login_redirect', array( $this, 'login_redirect' ), 10, 3 );
}

/**
* Add the settings page
*/
public function add_settings_page() {

// Add the settings page
add_options_page(
__( 'Login Redirect', 'login-redirect' ),
__( 'Login Redirect', 'login-redirect' ),
'manage_options',
'login_redirect',
array( $this, 'render_settings_page' )
);
}

/**
* Register the plugin settings
*/
public function register_settings() {

// Register the setting
register_setting( 'login_redirect_options', 'login_redirect_options', array( $this, 'validate_settings' ) );

// Add the settings section
add_settings_section(
'login_redirect_section',
__( 'Login Redirect Settings', 'login-redirect' ),
array( $this, 'render_settings_section' ),
'login_redirect'
);

// Add the settings field
add_settings_field(
'login_redirect_url',
__( 'Redirect URL', 'login-redirect' ),
array( $this, 'render_settings_field' ),
'login_redirect',
'login_redirect_section'
);
}

/**
* Validate the plugin settings
*
* @param array $settings The settings to validate
*
* @return array The validated settings
*/
public function validate_settings( $settings ) {

// Validate the settings
if ( isset( $settings['login_redirect_url'] ) && !empty( $settings['login_redirect_url'] ) ) {
$settings['login_redirect_url'] = esc_url( $settings['login_redirect_url'] );
} else {
$settings['login_redirect_url'] = home_url();
}

return $settings;
}

/**
* Render the settings page
*/
public function render_settings_page() {

// Check the user permissions
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.', 'login-redirect' ) );
}

// Include the settings template
include( plugin_dir_path( __FILE__ ) . 'templates/settings.php' );
}

/**
* Render the settings section
*/
public function render_settings_section() {

// Include the settings section template
include( plugin_dir_path( __FILE__ ) . 'templates/settings-section.php' );
}

/**
* Render the settings field
*/
public function render_settings_field() {

// Get the plugin settings
$settings = get_option( 'login_redirect_options', array() );

// Include the settings field template
include( plugin_dir_path( __FILE__ ) . 'templates/settings-field.php' );
}

/**
* Handle the login redirect
*
* @param string $redirect_to The default redirect URL
* @param string $requested_redirect_to The requested redirect URL
* @param WP_User $user The logged in user object
*
* @return string The redirect URL
*/
public function login_redirect( $redirect_to, $requested_redirect_to, $user ) {

// Get the plugin settings
$settings = get_option( 'login_redirect_options', array() );

// If the redirect URL is set, use it
if ( isset( $settings['login_redirect_url'] ) && !empty( $settings['login_redirect_url'] ) ) {
$redirect_to = $settings['login_redirect_url'];
}

return $redirect_to;
}
}

new Login_Redirect();

}