Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

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"].

  • Type PHP
  • Runs Where you put the [first_name] shortcode
  • Category Shortcodes
  • Tested with WordPress 7.1
PHP
<?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.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.