Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Last active July 20, 2021 09:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save andrewlimaza/1cd4e32dac6dbe870a64a5a426a14e71 to your computer and use it in GitHub Desktop.
Save andrewlimaza/1cd4e32dac6dbe870a64a5a426a14e71 to your computer and use it in GitHub Desktop.
Redirect Users After Login For WordPress
<?php
/**
* Redirect all non-admin user's after they login to your website's home page.
* Documentation for login_redirect filter - https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
* Visit https://yoohooplugins.com for more tutorials.
*/
function yh_redirect_after_login( $redirect_to, $request, $user ) {
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
//check if user is not an admin.
if ( !in_array( 'administrator', $user->roles ) ) {
$redirect_to = home_url();
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'yh_redirect_after_login', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment