Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dwanjuki/bf97c45ef09c09ac29c125a69d854bc9 to your computer and use it in GitHub Desktop.

Select an option

Save dwanjuki/bf97c45ef09c09ac29c125a69d854bc9 to your computer and use it in GitHub Desktop.
Redirect administrators to the WP Admin Dashboard and other users to the specified page on login.
<?php
/**
* Redirect Administrators to WP Admin,
* redirect all other users to the specified page after login.
*
* Ignores previously set redirects.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_login_redirect_users( $redirect_to, $request, $user ) {
if ( empty( $user ) || ! ( $user instanceof WP_User ) ) {
return $redirect_to;
}
if ( $user->has_cap( 'administrator' ) ) {
$redirect_to = admin_url();
} else {
$redirect_to = home_url( '/members/' );
}
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect_users', 99, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment