Last active
August 15, 2019 04:10
-
-
Save seothemes/7b4441bc6a30ecee9e1fcaef598b1039 to your computer and use it in GitHub Desktop.
Current user shortcode
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 | |
add_shortcode( 'current_user', __NAMESPACE__ . '\current_user_shortcode' ); | |
/** | |
* Description of expected behavior. | |
* | |
* @since 1.0.0 | |
* | |
* @param $atts | |
* | |
* @return mixed | |
*/ | |
function current_user_shortcode( $atts ) { | |
if ( is_admin() ) { | |
return false; | |
} | |
$atts = shortcode_atts( | |
[ | |
'avatar' => true, | |
'name' => true, | |
], | |
$atts, | |
'current_user' | |
); | |
$user = wp_get_current_user(); | |
$name = $user->user_firstname && $user->last_name ? $user->user_firstname . ' ' . $user->last_name : $user->first_name; | |
$html = sprintf( | |
'<div class="current-user">%s<small>%s</small></div>', | |
get_avatar( $user->ID ), | |
$name ? $name : __( 'Guest', 'current-user-shortcode' ) | |
); | |
return apply_filters( 'current_user_shortcode_output', $html, $atts, $user ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment