Redirect Users After Login with this class: CustomUserRedirect

Customuserredirect Redirect Users After Login Snipsnip Pro .code Snippets

You’ve got users with different needs. So you want to redirect them straight to the right place as soon as they login. Stop using a plugin that will display ads or will make an update one day that breaks your sites (happened to me today 🙁 ) (so I made this snippet).

Redirect your users upon login based on user roles. Easily add/remove/edit the custom roles and redirection rules. Include dynamic data, such as {{username}}, {{user_id}}, and more, including user meta with {{user_meta:META_KEY_HERE}}.

*Notes: This class assumes that each user has only one role. If a user has multiple roles, they will be redirected based on the first role found in their role list. Also, be aware of privacy considerations when using tokens like {{user_email}} and {{user_ip}}, as these could potentially expose sensitive user information.

What it does…

The CustomUserRedirect class is a PHP script designed to redirect users upon login in a WordPress site based on their user roles. This means that when a user logs into your WordPress site, they will be automatically redirected to a specific page depending on their role (e.g., Subscriber, Administrator, etc.). The class is highly customizable, allowing you to define your own redirect rules and use any custom user roles defined in your WordPress site.

Why it does it…

This functionality can be useful for providing a customized user experience. For example, you might want to direct subscribers to a specific content page, while administrators might be redirected to the dashboard or a specific administrative page. By customizing the post-login destination based on user roles, you can guide users to the most relevant parts of your site and improve their overall experience.

How it does it…

The class works by hooking into the wp_login action, which is triggered when a user logs into WordPress. When this action is triggered, the class’s redirectUser method is called, which checks the user’s roles and redirects them to the appropriate page.

The redirection rules are defined in an associative array, making it easy to add, modify, or remove the redirects for each role. You can use any user role defined in your WordPress site, including custom roles added by plugins or your theme.

Special tokens can be used in the URLs to include dynamic data. The class comes with a set of default tokens, including:

  • {{username}}: Replaced with the logged-in user’s username.
  • {{user_id}}: Replaced with the logged-in user’s ID.
  • {{current_page}}: Replaced with the current page the user was on when they submitted the login form.
  • {{website_url}}: Replaced with the URL of the website.
  • {{user_slug}}: Replaced with the user’s author URL slug or user nicename.
  • {{referrer}}: Replaced with the URL of the referring page.
  • {{user_ip}}: Replaced with the IP address of the user.
  • {{user_ip_hash}}: Replaced with the hashed IP address of the user.
  • {{user_email}}: Replaced with the user’s email address.
  • {{user_email_hash}}: Replaced with the hashed email address of the user.
  • {{user_registered}}: Replaced with the date and time when the user registered.
  • {{current_date}}: Replaced with the current date in ‘YYYYMMDD’ format.
  • {{current_time}}: Replaced with the current time in ‘HHMMSS’ format.
  • {{current_datetime}}: Replaced with the current date and time in ‘YYYYMMDDHHMMSS’ format.
  • {{user_meta:meta_key}}: Replaced with user meta (keep reading…)

For example, you could use the {{username}} token in a URL like /welcome/{{username}}, and it would be replaced with the logged-in user’s username.

You can also use custom user meta values in your URLs with the {{user_meta:meta_key}} token, where meta_key is the key of the user meta value you want to include. For example, you could use a token like {{user_meta:first_name}} to include the user’s first name in the URL.

Furthermore, you can create your own custom tokens by adding them to the replaceTokens method and defining their replacement values.

It’s important to note that this class assumes that each user has only one role. If a user has multiple roles, they will be redirected based on the first role found in their role list.

Also, be aware of privacy considerations when using tokens like {{user_email}} and {{user_ip}}, as these could potentially expose sensitive user information. Always handle sensitive user data with care, and comply with all relevant privacy laws and regulations.

Use the JSON file for easily importing into your CodeSnippetsPro plugin. Use the PHP file or just copy/paste the code sample into your functions.php if you’re not using CodeSnippetsPro.

Related Snips

See the code on github…

Leave a Reply

Your email address will not be published. Required fields are marked *