Forked from ipokkel/pmpro-redirect-to-user-page.php
Last active
April 23, 2025 13:22
-
-
Save davidmutero/e24d1fb73e19f9fc31d431893c6b797d to your computer and use it in GitHub Desktop.
Plugin to work with PMPro and PMPro User Pages to redirect someone to their specific (with URL) user page on login
This file contains hidden or 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 | |
/** | |
* This recipe redirects members to their Specific (with URL) User Page on login | |
* and non-members to the PMPro membership levels page. | |
* | |
* This recipe assumes the User Pages Add On is installed and configured. | |
* @link https://www.paidmembershipspro.com/add-ons/pmpro-user-pages/ | |
* | |
* 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_pmproup_login_redirect_to_full_user_page( $redirect_to, $request, $user ) { | |
// Exit early if PMPro is not active. | |
if ( ! defined( 'PMPRO_VERSION' ) ) { | |
return $redirect_to; | |
} | |
global $wpdb; | |
// Check that we have a valid user who is not an admin. | |
if ( ! empty( $user ) && ! empty( $user->ID ) && ! current_user_can( 'manage_options', $user->ID ) ) { | |
// Check if the user has an active membership. | |
$is_member = $wpdb->get_var( $wpdb->prepare( | |
"SELECT membership_id FROM $wpdb->pmpro_memberships_users WHERE status = 'active' AND user_id = %d LIMIT 1", | |
$user->ID | |
) ); | |
if ( $is_member ) { | |
// Get the ID of the user's User Page. | |
$user_page_id = get_user_meta( $user->ID, 'pmproup_user_page', true ); | |
if ( ! empty( $user_page_id ) ) { | |
// Build the nested URL (e.g., /top-level-page/username/specific_url/). | |
$user_page_url = get_permalink( $user_page_id ); | |
if ( ! empty( $user_page_url ) ) { | |
$redirect_to = trailingslashit( $user_page_url ) . 'specific_url/'; // Append /specific_url/ manually | |
} | |
} | |
} else { | |
// Non-members are redirected to the membership levels page. | |
$redirect_to = pmpro_url( 'levels' ); | |
} | |
} | |
return $redirect_to; | |
} | |
add_filter( 'login_redirect', 'my_pmproup_login_redirect_to_full_user_page', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment