Skip to content

Instantly share code, notes, and snippets.

@stephenharris
Last active December 11, 2015 08:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenharris/4573623 to your computer and use it in GitHub Desktop.
Save stephenharris/4573623 to your computer and use it in GitHub Desktop.
Using WP-Member to hide / show content depending on various conditions.
<?php
/**
* Using WP-Member to hide / show content depending on various conditions.
*
* This displaying content depending on:
* * Whether the member has **only** the guest level attached to them (this includes, but not restricted to logged out users)
* * Whether the member has the level 'Some Level' attached to theme
* * Otherwise if the user is logged in, displays a different message.
*
*/
$user_id = get_current_user_id(); //The User ID, Will be 0 for logged out users
$user_level_ids = wpmember_get_user_membership( $user_id, 'level_ids' ); //Array of membership level IDs;
$guest_id = (int) get_option('default_wmlevel'); //The ID of the guest level (assigned automatically to non-members).
//Next you need to get the 'allowed' level ID. (If the ID is known, just use that - otherwise we can get it from the level name).
$allowed_level = get_term_by('name', 'Some Level', 'wmlevel');
if( $allowed_level ){
$allowed_level_id = $allowed_level->term_id;
}else{
echo 'The level could not be found.</br>';
$allowed_level_id = -1;
}
if( $user_level_ids == array( $guest_id ) ) {
echo 'This user has only the guest level assigned to them. They could be logged out.</br>';
echo 'They may be logged in, and they may even be an active member if you have assigned some members to only the guest level</br>';
echo '<strong>Logged out users will have 0 as their user ID</strong></br>';
} elseif ( wpmember_user_can_access( $user_id, array( $allowed_level_id ) ) ){
echo 'This user has the appropriate level. (Note admins are not restricted).';
}elseif( $user_id !== 0 ){
echo 'This user is logged in and is not just assigned to the guest level, but doesn\'t have the appropriate level';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment