Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Created December 5, 2024 17:36
Show Gist options
  • Save kimcoleman/0dafe3c83ab184054970627d749911d3 to your computer and use it in GitHub Desktop.
Save kimcoleman/0dafe3c83ab184054970627d749911d3 to your computer and use it in GitHub Desktop.
Redirection for the User Pages Add On when you do not want to set the Top Level Page in Add On settings.
<?php
/**
* Redirection for the User Pages Add On.
*
* Note: This is only needed when you do not want to set the Top Level Page in Add On settings.
* If you have set a Top Level Page, the Add On has native logic for redirection.
*/
function my_custom_user_page_template_redirect() {
// Exit early if not on the control page.
if ( ! is_page( 'clients' ) ) {
return;
}
// Get the fallback redirection URL.
global $pmpro_pages, $current_user;
$no_user_page_redirect_url = home_url();
if ( ! empty( $pmpro_pages['levels'] ) && get_post_status( $pmpro_pages['levels'] ) === 'publish' ) {
$no_user_page_redirect_url = get_permalink( $pmpro_pages['levels'] );
}
// Get the logged-in user's User Page, if assigned.
$user_page_id = is_user_logged_in() ? get_user_meta( $current_user->ID, 'pmproup_user_page', true ) : false;
// Redirect to the user's page if it exists and is published.
if ( ! empty( $user_page_id ) && get_post_status( $user_page_id ) === 'publish' ) {
wp_safe_redirect( get_permalink( $user_page_id ) );
exit;
}
// Redirect to the fallback URL.
wp_safe_redirect( $no_user_page_redirect_url );
exit;
}
add_action( 'template_redirect', 'my_custom_user_page_template_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment