Skip to content

Instantly share code, notes, and snippets.

@eleanorcode
Last active December 2, 2021 22:04
Show Gist options
  • Select an option

  • Save eleanorcode/f1bdd6fc2143c9fe9d1d329c5919b7fb to your computer and use it in GitHub Desktop.

Select an option

Save eleanorcode/f1bdd6fc2143c9fe9d1d329c5919b7fb to your computer and use it in GitHub Desktop.
Redirect Users after Login in WordPress
<?php
function custom_login_redirect( $redirect_to, $request, $user ) {
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) || in_array( 'editor', $user->roles ) || in_array( 'author', $user->roles ) ) {
$redirect_to = admin_url();
} else if ( in_array( 'customer', $user->roles ) || in_array( 'shop_manager', $user->roles ) ) {
$redirect_to = home_url( '/shop' );
} else {
$redirect_to = home_url();
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment