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/d0b862e44447fe1efa0d9787a08d5029 to your computer and use it in GitHub Desktop.
Save ipokkel/d0b862e44447fe1efa0d9787a08d5029 to your computer and use it in GitHub Desktop.
Redirects users to their PMPro account page on login from home page / site root. #paid-memberships-pro #login #redirect
<?php
/**
* This recipe redirects users to their PMPro account page on login
* from the home page (site root).
*
* To set a referrer for the redirect_to variable for logged-out users
* refer to this article.
* https://www.paidmembershipspro.com/redirect-members-to-referrer-on-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 redirect_after_login_to_account_1610959060406( $redirect_to, $requested_redirect_to, $user ) {
if ( home_url() === $redirect_to || home_url() . '/' === $redirect_to ) {
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
//check if user is not an admin.
if ( ! in_array( 'administrator', $user->roles ) ) {
if ( function_exists( 'pmpro_hasMembershipLevel' ) ) {
$redirect_to = pmpro_url( 'account' );
}
}
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'redirect_after_login_to_account_1610959060406', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment