Skip to content

Instantly share code, notes, and snippets.

@seothemes
Last active August 15, 2019 04:10
Show Gist options
  • Save seothemes/7b4441bc6a30ecee9e1fcaef598b1039 to your computer and use it in GitHub Desktop.
Save seothemes/7b4441bc6a30ecee9e1fcaef598b1039 to your computer and use it in GitHub Desktop.
Current user shortcode
<?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