We Build Our Own Custom Login Redirect Plugin

Keys on a ring and a wooden signpost, a cobalt-blue sphere between themAI-generated
Contents

How to Create a WordPress Login Redirect Plugin – A Step-by-Step Guide

Introduction

Developing a custom WordPress plugin is an excellent way to tailor your website to your needs while gaining valuable knowledge of WordPress development. In this post we show you how to create a login redirect plugin that sends users to specific pages after logging in, based on their role.

With this guide you will:

  • Write a working WordPress plugin
  • Redirect users based on their role
  • Create an admin menu to manage the redirects

Let’s get started!

1. Create the plugin base

Every WordPress plugin starts with a file in the wp-content/plugins/ directory. Create a new folder custom-login-redirect there and a file custom-login-redirect.php.

Open the file and add this code:

<?php
/**
 * Plugin Name: Custom Login Redirect
 * Plugin URI: https://example.com
 * Description: Leitet Benutzer nach dem Login je nach Rolle auf eine bestimmte Seite weiter.
 * Version: 1.0
 * Author: Dein Name
 * Author URI: https://example.com
 */

if (!defined('ABSPATH')) {
    exit; // Sicherheit: Direktzugriff verhindern
}

2. Redirect users after login

Now we implement the plugin’s core function: redirecting users based on their role. For this we use the wp_login hook.

Add the following code below the plugin header:

function clr_redirect_user_after_login($user_login, $user) {
    // Standard-Weiterleitung
    $redirect_url = home_url();
    
    // Rolle des Benutzers abrufen
    if (in_array('administrator', $user->roles)) {
        $redirect_url = admin_url();
    } elseif (in_array('editor', $user->roles)) {
        $redirect_url = site_url('/editor-dashboard');
    } elseif (in_array('subscriber', $user->roles)) {
        $redirect_url = site_url('/willkommen');
    }
    
    wp_safe_redirect($redirect_url);
    exit;
}
add_action('wp_login', 'clr_redirect_user_after_login', 10, 2);

What the code does:

  • The function checks the user’s role after login.
  • Based on the role, a specific redirect URL is set.
  • wp_safe_redirect($redirect_url) redirects the user to the desired page.

3. Admin page for custom redirects

Now we add a settings page to the WordPress admin area where administrators can change the redirect targets.

Add this code to your plugin file:

function clr_add_admin_menu() {
    add_options_page('Login Redirect Einstellungen', 'Login Redirect', 'manage_options', 'clr-settings', 'clr_settings_page');
}
add_action('admin_menu', 'clr_add_admin_menu');

function clr_settings_page() {
    ?>
    <div class="wrap">
        <h1>Login Redirect Einstellungen</h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('clr_settings_group');
            do_settings_sections('clr-settings');
            submit_button();
            ?>
        </form>
    </div>
    <?php
}

What the code does:

  • The function clr_add_admin_menu() adds a new menu item under Settings.
  • The function clr_settings_page() generates the HTML structure of the settings page.

4. Make the redirect targets savable

Now we implement the save function so the redirect targets can be stored in the WordPress database.

Add this code:

function clr_register_settings() {
    register_setting('clr_settings_group', 'clr_redirect_admin');
    register_setting('clr_settings_group', 'clr_redirect_editor');
    register_setting('clr_settings_group', 'clr_redirect_subscriber');
}
add_action('admin_init', 'clr_register_settings');

Now we update the clr_settings_page() function so the saved values are displayed:

function clr_settings_page() {
    ?>
    <div class="wrap">
        <h1>Login Redirect Einstellungen</h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('clr_settings_group');
            do_settings_sections('clr-settings');
            ?>
            <label>Administrator Weiterleitung:</label>
            <input type="text" name="clr_redirect_admin" value="<?php echo esc_attr(get_option('clr_redirect_admin', admin_url())); ?>">
            <br><br>
            <label>Editor Weiterleitung:</label>
            <input type="text" name="clr_redirect_editor" value="<?php echo esc_attr(get_option('clr_redirect_editor', site_url('/editor-dashboard'))); ?>">
            <br><br>
            <label>Subscriber Weiterleitung:</label>
            <input type="text" name="clr_redirect_subscriber" value="<?php echo esc_attr(get_option('clr_redirect_subscriber', site_url('/willkommen'))); ?>">
            <br><br>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

5. Integrate the plugin settings into the redirect logic

Now we change the clr_redirect_user_after_login() function so that it uses the values saved in the settings:

function clr_redirect_user_after_login($user_login, $user) {
    $redirect_url = home_url();

    if (in_array('administrator', $user->roles)) {
        $redirect_url = get_option('clr_redirect_admin', admin_url());
    } elseif (in_array('editor', $user->roles)) {
        $redirect_url = get_option('clr_redirect_editor', site_url('/editor-dashboard'));
    } elseif (in_array('subscriber', $user->roles)) {
        $redirect_url = get_option('clr_redirect_subscriber', site_url('/willkommen'));
    }

    wp_safe_redirect($redirect_url);
    exit;
}

Conclusion

We have built a complete login redirect plugin that redirects users based on their role. We also created an admin interface to change the redirects dynamically.

Here is the complete code, which you can copy into the newly created file custom-login-redirect.php in the new folder custom-login-redirect inside the wp-content/plugins/ directory.

<?php
/**
 * Plugin Name: Custom Login Redirect
 * Plugin URI: https://example.com
 * Description: Leitet Benutzer nach dem Login je nach Rolle auf eine bestimmte Seite weiter.
 * Version: 1.0
 * Author: Dein Name
 * Author URI: https://example.com
 */

if (!defined('ABSPATH')) {
    exit; // Sicherheit: Direktzugriff verhindern
}

function clr_redirect_user_after_login($user_login, $user) {
    $redirect_url = home_url();

    if (in_array('administrator', $user->roles)) {
        $redirect_url = get_option('clr_redirect_admin', admin_url());
    } elseif (in_array('editor', $user->roles)) {
        $redirect_url = get_option('clr_redirect_editor', site_url('/editor-dashboard'));
    } elseif (in_array('subscriber', $user->roles)) {
        $redirect_url = get_option('clr_redirect_subscriber', site_url('/willkommen'));
    }

    wp_safe_redirect($redirect_url);
    exit;
}

add_action('wp_login', 'clr_redirect_user_after_login', 10, 2);

function clr_add_admin_menu() {
    add_options_page('Login Redirect Einstellungen', 'Login Redirect', 'manage_options', 'clr-settings', 'clr_settings_page');
}
add_action('admin_menu', 'clr_add_admin_menu');

function clr_register_settings() {
    register_setting('clr_settings_group', 'clr_redirect_admin');
    register_setting('clr_settings_group', 'clr_redirect_editor');
    register_setting('clr_settings_group', 'clr_redirect_subscriber');
}
add_action('admin_init', 'clr_register_settings');

function clr_settings_page() {
    ?>
    <div class="wrap">
        <h1>Login Redirect Einstellungen</h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('clr_settings_group');
            do_settings_sections('clr-settings');
            ?>
            <label>Administrator Weiterleitung:</label>
            <input type="text" name="clr_redirect_admin" value="<?php echo esc_attr(get_option('clr_redirect_admin', admin_url())); ?>">
            <br><br>
            <label>Editor Weiterleitung:</label>
            <input type="text" name="clr_redirect_editor" value="<?php echo esc_attr(get_option('clr_redirect_editor', site_url('/editor-dashboard'))); ?>">
            <br><br>
            <label>Subscriber Weiterleitung:</label>
            <input type="text" name="clr_redirect_subscriber" value="<?php echo esc_attr(get_option('clr_redirect_subscriber', site_url('/willkommen'))); ?>">
            <br><br>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

?>

Michael Rademacher

Founder of realmaker

Michael Rademacher founded realmaker in 2003. He develops WordPress websites and software, produces films and live streams, and writes here about what he learns in client projects.

About Michael

Rather have it maintained?

Updates, backups and monitoring are handled by wp-einfach.de – for a fixed monthly price (service in German).

Go to wp-einfach.de