Skip to content

Instantly share code, notes, and snippets.

@dwanjuki
Last active May 14, 2025 12:25
Show Gist options
  • Save dwanjuki/e98ee47ea275de4f704037bd15a63cf3 to your computer and use it in GitHub Desktop.
Save dwanjuki/e98ee47ea275de4f704037bd15a63cf3 to your computer and use it in GitHub Desktop.
Redirect users with Gift Purchases to the Membership Account page on login
<?php
/**
* Redirect users with Gift Purchases to the Membership Account page on login
* if no redirect has been set via the redirect_to URL parameter
*
* 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_gift_purchaser_login_redirect( $redirect_to, $requested_redirect_to, $user ) {
// bail if PMPro or Gift Levels Add On is not active
if ( ! function_exists( 'pmpro_url' ) || ! function_exists( 'pmprogl_pmpro_after_checkout' ) ) {
return $redirect_to;
}
// bail if no user
if ( empty( $user ) || empty( $user->ID ) ) {
return $redirect_to;
}
// bail if a redirect destination was passed to login
if ( ! empty( $requested_redirect_to ) ) {
return $redirect_to;
}
// get account page URL
$account_page_url = pmpro_url( 'account' );
// check if the user was not being redirecting to the account page
if ( $redirect_to !== $account_page_url ) {
// check if they had purchased a gift membership
$gift_codes = get_user_meta( $user->ID, 'pmprogl_gift_codes_purchased', true );
if ( ! empty( $gift_codes ) ) {
// change login redirect location to Membership Account page
$redirect_to = $account_page_url;
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'my_pmpro_gift_purchaser_login_redirect', 20, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment