Visitor’s first name shortcode
Type [first_name] to greet logged-in users by name (“Welcome back, Maria!”). Visitors see nothing, or the fallback you set: [first_name fallback="friend"].
<?php
// [first_name] or [first_name fallback="friend"]
$atts = shortcode_atts( array( 'fallback' => '' ), $atts, 'first_name' );
$user = wp_get_current_user();
$name = $user->exists() ? ( $user->first_name ? $user->first_name : $user->display_name ) : $atts['fallback'];
return esc_html( $name );
Good to know
Page caches must skip logged-in users (most do), or one visitor’s name could be shown to others.
How to use it
Type [first_name] in a post, a page or a Shortcode block.
In these packs
- Shortcodes: Handy shortcodes for dates, breadcrumbs, buttons, share links, members-only content and more.