Personal Greeting Shortcode
Adds a [wpide_hello] shortcode that greets logged-in members by first name ("Hi, Sam") and visitors with a fallback you choose.
Opens WPIDE on
Code
add_shortcode( 'wpide_hello', function ( $atts ) {
$atts = shortcode_atts( array( 'greeting' => 'Hi', 'guest' => 'there' ), $atts, 'wpide_hello' );
$name = $atts['guest'];
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$name = $user->first_name ? $user->first_name : $user->display_name;
}
return esc_html( sprintf( '%s, %s', $atts['greeting'], $name ) );
} );
Notes
Example: [wpide_hello greeting="Welcome back" guest="friend"]. Pages with personal greetings should not be served from a full-page cache to logged-in users.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.