Toolsnip

Wordpress: Redirect User After Login

Customize user experience and enhance security with this WordPress snippet that redirects users based on their role after login.

Redirecting users after login based on their role is a common requirement in many WordPress projects. This snippet facilitates a custom redirection process, ensuring that users are directed to the appropriate part of the site after logging in.

The wp_login hook is utilized to intercept the login process, and a conditional check is performed to redirect users based on their roles. This can help in directing administrators to the dashboard and subscribers to a custom welcome page.

Such redirections not only enhance security by limiting access to certain parts of the site but also improve the user experience by taking users directly to the most relevant section.

The snippet includes examples of how to handle different user roles with different redirection paths, offering a versatile approach that can be adapted to various needs.

It also discusses the importance of securing redirection processes to prevent open redirect vulnerabilities, ensuring that the site remains secure against common web threats.

Snippet Code

Version Compatibility

This code is optimized for WordPress 3.0 and above, where the wp_login hook and role-based redirection are fully supported.

Required PHP Version

PHP 5.6 or higher

Wordpress Core Functions Used

Use Cases

  • user management
  • custom workflows
  • security enhancements