Last active
August 28, 2020 13:30
-
-
Save ronalfy/4d288ba066af0a40ef6f58b6346b8e86 to your computer and use it in GitHub Desktop.
PMPro - Template for User Pages Based on Level ID.
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 | |
/** | |
* 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