Skip to content

Instantly share code, notes, and snippets.

@LMNTL
Created July 22, 2019 18:44
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 LMNTL/b2f52114b960134f137f293e9c4a2583 to your computer and use it in GitHub Desktop.
Save LMNTL/b2f52114b960134f137f293e9c4a2583 to your computer and use it in GitHub Desktop.
show a 404 page when trying to access a profile page for members with "Hide from Directory?" checked and/or don't have a specific level
//show a 404 page when trying to access a profile page for members with "Hide from Directory?" checked and/or don't have a specific level
function my_pmpromd_profile_preheader()
{
// -------- Edit this line to determine which levels get profile pages ---------- //
$directory_levels = array(1, 2, 3);
global $post, $pmpro_pages, $current_user, $wp_query;
if(!empty($post->ID) && $post->ID == $pmpro_pages['profile'])
{
//Get the profile user
if(!empty($_REQUEST['pu']) && is_numeric($_REQUEST['pu']))
$pu = get_user_by('id', $_REQUEST['pu']);
elseif(!empty($_REQUEST['pu']))
$pu = get_user_by('slug', $_REQUEST['pu']);
elseif(!empty($current_user->ID))
$pu = $current_user;
else
$pu = false;
//Get the level for the profile user
$level = pmpro_getMembershipLevelForUser($pu->ID);
//If the profile user has "Hide from Directory?" checked or doesn't have an approved level, show a 404 page
if(!empty($pu->pmpromd_hide_directory) || !isset($level->ID) || !in_array( $level->ID, $directory_levels ) )
{
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit;
}
}
}
add_action("wp", "my_pmpromd_profile_preheader", 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment