Created
April 5, 2024 08:21
-
-
Save ipokkel/86ef28a291000b2def77240ac04c60ac to your computer and use it in GitHub Desktop.
Redirect administrators and membership manangers to the WP Admin Dashboard and other users to the membership account page on login.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Redirect Administrators and Membership Managers to WP Admin, | |
* redirect other users to the account page after login. | |
* | |
* 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_pmpro_login_redirect_admin_and_users( $redirect_to, $request, $user ) { | |
// If you'd like to honor previously set redirects, uncomment this section. | |
/* | |
if ( ! empty( $redirect_to ) ) { | |
return $redirect_to; | |
} | |
*/ | |
if ( ! is_wp_error( $user ) ) { | |
$roles = $user->roles; | |
if ( in_array( 'administrator', $roles, true ) || in_array( 'membership_manager', $roles, true ) ) { | |
$redirect_to = admin_url(); | |
} else { | |
if ( function_exists( 'pmpro_url' ) ) { | |
$redirect_to = pmpro_url( 'account' ); | |
} else { | |
$redirect_to = home_url(); | |
} | |
} | |
} | |
return $redirect_to; | |
} | |
add_filter( 'login_redirect', 'my_pmpro_login_redirect_admin_and_users', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment