Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/86ef28a291000b2def77240ac04c60ac to your computer and use it in GitHub Desktop.
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.
<?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