Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidmutero/a1a71aea6ba343e3a7de2a1d131d1796 to your computer and use it in GitHub Desktop.
Save davidmutero/a1a71aea6ba343e3a7de2a1d131d1796 to your computer and use it in GitHub Desktop.
Redirect Logged-In Users to Their Full Level User Page from the Top Level Parent Page
<?php
/**
* Redirect logged-in members visiting /top-level-page/ to their full user level page. Works best for single level setup.
* (e.g., /top-level-parent-page/username/user-page/) immediately, skipping the intermediate user page.
*
* This improves UX by automatically taking users to their specific level user page
* without requiring a second click.
*
* 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.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmproup_redirect_on_parent_user_page_visit() {
if ( ! is_user_logged_in() || is_admin() || ! function_exists( 'pmproup_getOptions' ) ) {
return;
}
global $post, $current_user;
$options = pmproup_getOptions();
// Check if we're on the parent user page.
if ( ! empty( $post ) && ! empty( $options['parent_page'] ) && intval( $post->ID ) === intval( $options['parent_page'] ) ) {
// Only redirect non-admin users.
if ( ! current_user_can( 'manage_options', $current_user->ID ) ) {
$user_page_id = get_user_meta( $current_user->ID, 'pmproup_user_page', true );
if ( ! empty( $user_page_id ) ) {
$user_page_url = get_permalink( $user_page_id );
if ( ! empty( $user_page_url ) ) {
// Redirect directly to the user's level-specific page.
wp_redirect( trailingslashit( $user_page_url ) . 'level-user-page/' );
exit;
}
}
}
}
}
add_action( 'wp', 'my_pmproup_redirect_on_parent_user_page_visit', 9 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment