Created
June 4, 2018 10:39
-
-
Save DavidPeralvarez/96e2565017cbf386b34cd0be9a064e63 to your computer and use it in GitHub Desktop.
Cómo obtener datos de un usuario de WordPress
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 | |
/* | |
Plugin Name: MemberPerks | |
Description: Ofrece beneficios a tus usuarios más leales | |
Author: David Perálvarez | |
Version: 1.0.0 | |
Author URI: https://silicodevalley.com | |
*/ | |
// Create the shortcode | |
add_shortcode( 'show_member_level', 'mp_show_member_level'); | |
// Get the member level | |
function mp_show_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; | |
ob_start(); | |
?> | |
<p class="mp-member-level"> | |
<?php echo $memberName; ?> | |
tu <strong>nivel de perturbación en la fuerza es de | |
<?php echo $memberLevel; ?></strong>, | |
pronto te explicaré de qué va esto :) | |
</p> | |
<?php | |
$output = ob_get_contents(); | |
ob_end_clean(); | |
return $output; | |
else: | |
return; // User is not logged in | |
endif; | |
} | |
// Include styles | |
add_action( 'wp_enqueue_scripts', 'mp_add_styles' ); | |
function mp_add_styles(){ | |
wp_register_style( 'mp-styles', plugin_dir_url(__FILE__) . 'assets/css/mp-styles.css' ); | |
wp_enqueue_style( 'mp-styles' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment