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/a64e0a47884e2eb66fcd8915250bab12 to your computer and use it in GitHub Desktop.
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.
<?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