Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PMPro - Template for User Pages Based on Level ID.
<?php
/**
* Load a page template for a user page based on level.
*/
function pmpro_user_page_redirect_per_level() {
// Make sure user is logged in.
if ( ! is_user_logged_in() ) {
return;
}
// Make sure we are on a page.
if ( ! is_page() ) {
return;
}
global $post, $current_user;
// Get user meta and see if parent page matches up user page.
$user_page_id = get_user_meta( $current_user->ID, 'pmproup_user_page', true );
if ( $post->post_parent != $user_page_id ) {
return;
}
// Retrieve the level object and assign level ID.
$level_object = pmpro_getMembershipLevelForUser( $current_user->ID );
if ( ! $level_object || ! isset( $level_object->ID ) ) {
return;
}
$level_id = $level_object->ID;
// If certain level is matched, load a template from the theme.
switch ( $level_id ) {
case '10':
$file = get_stylesheet_directory() . '/level-10-template.php';
if ( file_exists( $file ) ) {
include $file;
exit;
}
break;
default:
break;
}
}
add_action( 'template_redirect', 'pmpro_user_page_redirect_per_level' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment