Forked from strangerstudios/pmpro-redirect-to-user-page.php
Last active
April 23, 2025 12:51
-
-
Save ipokkel/a64e0a47884e2eb66fcd8915250bab12 to your computer and use it in GitHub Desktop.
Plugin to work with PMPro and PMPro User Pages to redirect someone to their latest 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 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_user_page( $redirect_to, $request, $user ) { | |
// Bail if no PMPro | |
if ( ! defined( 'PMPRO_VERSION' ) ) { | |
return $redirect_to; | |
} | |
global $wpdb; | |
$is_logged_in = ! empty( $user ) && ! empty( $user->ID ); | |
// Check that user is logged in and not Administrator. | |
if ( $is_logged_in && ! current_user_can( 'manage_options' ) ) { | |
// Can't use the pmpro_hasMembershipLevel function because it won't be defined yet. | |
$is_member = $wpdb->get_var( "SELECT membership_id FROM $wpdb->pmpro_memberships_users WHERE status = 'active' AND user_id = '" . esc_sql( $user->ID ) . "' LIMIT 1" ); | |
if ( $is_member ) { | |
//check for a user page | |
$user_page_id = get_user_meta( $user->ID, 'pmproup_user_page', true ); | |
//get user page url | |
if ( ! empty( $user_page_id ) ) { | |
//make sure the page exists | |
$url = get_permalink( $user_page_id ); | |
if ( ! empty( $url ) ) { | |
$redirect_to = $url; | |
} | |
} | |
} else { | |
// send non-members to the membership levels page | |
$url = get_permalink( pmpro_url( 'levels' ) ); | |
if ( ! empty( $url ) ) { | |
$redirect_to = $url; | |
} | |
} | |
} | |
return $redirect_to; | |
} | |
add_filter( 'login_redirect', 'my_pmproup_login_redirect_to_user_page', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment