Skip to content

Instantly share code, notes, and snippets.

@DavidPeralvarez
Last active June 13, 2018 07:31
Show Gist options
  • Save DavidPeralvarez/8e95900b9de97da0c9c0ff4f36b80e62 to your computer and use it in GitHub Desktop.
Save DavidPeralvarez/8e95900b9de97da0c9c0ff4f36b80e62 to your computer and use it in GitHub Desktop.
MemberPerks mp_get_member_level
<?php
// Function to get the member level
function mp_get_member_level(){
// Get current user's ID
$memberID = get_current_user_id();
if( $memberID != 0 ):
// Get member's info
$memberInfo = get_userdata($memberID);
$memberName = $memberInfo->first_name;
if(empty($memberName)):
$memberName = $memberInfo->user_login;
endif;
// Registration date
$memberRegistered = $memberInfo->user_registered;
$registrationDate = new DateTime($memberRegistered);
// Current Date
$currentDate = new DateTime("now", new DateTimeZone('Europe/Madrid'));
//https://stackoverflow.com/questions/4233605/elegant-way-to-get-the-count-of-months-between-two-dates
$memberLevel = $registrationDate->diff($currentDate)->m + ($registrationDate->diff($currentDate)->y*12) + 1;
$output = array(
'member_name' => $memberName,
'member_level' => $memberLevel
);
return $output;
else:
return; // User is not logged in
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment