Skip to content

Instantly share code, notes, and snippets.

@AnadarProSvcs
Last active December 19, 2023 18:58
Show Gist options
  • Save AnadarProSvcs/5ebb8c67781854ce641e46cc2b656c6a to your computer and use it in GitHub Desktop.
Save AnadarProSvcs/5ebb8c67781854ce641e46cc2b656c6a to your computer and use it in GitHub Desktop.
Redirect where a user goes when they log out of WordPress depending on their role. #wordpress
function custom_logout_redirect( $redirect_to, $requested_redirect_to, $user ) {
// Array of roles and their respective redirect URLs
$role_redirects = array(
'administrator' => home_url( '/admin-page/' ),
'editor' => home_url( '/editor-page/' ),
// Add other roles and their redirects here
);
// Check if user data is available
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
// Check if the user's role is in the array and redirect accordingly
if ( array_key_exists( $role, $role_redirects ) ) {
return $role_redirects[$role];
}
}
}
// Default redirect to homepage for roles not specified in the array
return home_url();
}
add_filter( 'logout_redirect', 'custom_logout_redirect', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment