Skip to content

Instantly share code, notes, and snippets.

@pbrocks
Created June 13, 2018 23:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbrocks/860f308f53fed5f3d8bdbc920289d12c to your computer and use it in GitHub Desktop.
Save pbrocks/860f308f53fed5f3d8bdbc920289d12c to your computer and use it in GitHub Desktop.
Replacement redirect for logins after TML update. This recipe will redirect PMPro admins to dashboard reports, single out subscribers to go to the Account page and everyone else to the Invoice page.
<?php
/**
* Redirect user based on user role after successful login.
*
* $pmpro_pages returns an array:
* * $pmpro_pages['account']
* * $pmpro_pages['billing']
* * $pmpro_pages['cancel']
* * $pmpro_pages['checkout']
* * $pmpro_pages['confirmation']
* * $pmpro_pages['invoice']
* * $pmpro_pages['levels']
*
* @param string $redirect_to URL to redirect to.
* @param string $request URL the user is coming from.
* @param object $user Logged user's data.
* @return string
*/
function replacement_login_redirect( $redirect_to, $request, $user ) {
global $pmpro_pages;
// is there a user to check?
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) ) {
// redirect them to dashboard reports page
$redirect_to = admin_url( 'admin.php?page=pmpro-reports' );
}
// check for subscribers
elseif ( in_array( 'subscriber', $user->roles ) ) {
// redirect them to account page
$redirect_to = get_permalink( $pmpro_pages['account'] );
} else {
// redirect them to account page
$redirect_to = get_permalink( $pmpro_pages['invoice'] );
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'replacement_login_redirect', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment