Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daddiofaddio/c0485d2fa846b0f74884203d483256c4 to your computer and use it in GitHub Desktop.
Save daddiofaddio/c0485d2fa846b0f74884203d483256c4 to your computer and use it in GitHub Desktop.
Display username, message & avatar shortcode with link to My Account page - conditional based on existing user fields
/* DISPLAY AVATAR & USER SHORTCODE - CONDITIONAL */
function display_current_user_shortcode()
{
global $current_user;
$user = wp_get_current_user();
$avatar = get_avatar($current_user->ID, 32, '', '', $args = array('class' => 'rounded-circle'));
$custom_avatar = get_avatar('https://url-to-custom-avatar.webp');
$first = $user->user_firstname;
$display = $user->display_name;
$email = $user->user_email;
if (is_user_logged_in()) {
if (!empty($display)) {
return $avatar . '<a href="https://site-url.com/my-account/"' . 'class="">' . 'Welcome, ' . esc_html($display) . '!' . '</a>';
} else {
return $custom_avatar . '<a href="https://site-url.com/my-account/"' . 'class="">' . 'Welcome, ' . esc_html($display) . '!' . '</a>';
}
} elseif (is_user_logged_in()) {
if (empty($display) && (!empty($first))) {
return $avatar . '<a href="https://site-url.com/my-account/"' . 'class="">' . 'Welcome, ' . esc_html($first) . '!' . '</a>';
} else {
return $custom_avatar . '<a href="https://site-url.com/my-account/"' . 'class="">' . 'Welcome, ' . esc_html($first) . '!' . '</a>';
}
} elseif (is_user_logged_in()) {
if (empty($display) && (empty($first)) && (!empty($email))) {
return $avatar . '<a href="https://site-url.com/my-account/"' . 'class="">' . 'Welcome, ' . esc_html($email) . '!' . '</a>';
} else {
return $custom_avatar . '<a href="https://site-url.com/my-account/"' . 'class="">' . 'Welcome, ' . esc_html($email) . '!' . '</a>';
}
}
}
add_shortcode('current-user', 'display_current_user_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment