• Is there any snippet so that I can display the logged in user’s first name only in a page/post using a shortcode.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @silumant,

    You can try a custom shortcode. Here’s an example.

    
    add_shortcode( 'my_first_name', function ( $atts ) {
    	$current_user = wp_get_current_user();
    	if ( ! ( $current_user instanceof WP_User ) ) return;
    	
            $atts = array_change_key_case( (array) $atts, CASE_LOWER );
     
    	extract( shortcode_atts( array(
        	  'display_tag' => 'h2',
            ), $atts ) );
    	
            $o = '<div class="wporg-box">';
            $t = $display_tag ? $display_tag : 'h2'; // Just in case.
            $o .= '<' . $t . '>' . esc_html__( $current_user->user_firstname )  . '</' . $t . '>';
            $o .= '</div>';
     
            return $o;
    } );
    

    Add this to your child theme’s functions.php file or use a code snippets plugin.

    Usage: [my_first_name], [my_first_name display_tag="h1"]

    Hope that helps ??

    Thread Starter Mr. Deb

    (@silumant)

    Thank you for reply @mlchaves . I tried the snippet & it is working fine in showing the first name??. But there is a problem. When I try to use it inside the classic editor, inside a text, it is not aligned properly.

    See this screenshot: https://prnt.sc/GoLUJiPqLTYD

    Here I have added the shortcode inside a sentence to show the firs name in the sentence; but the line is breaking like the screenshot above. Ay way to solve this?

    @silumant thanks for creating this post and @mlchaves many many thanks, man

    hi, this is just what I need. so what I want to do is say “Welcome back, [my_first_name]” but when i do it, it puts the shortcode first name in a new line, I’d like to have it all in one line. Any help, I cannot programme.

    Hello @silumant and @mugatea ,

    The answer to your questions is a CSS exercise. I’d need to see your pages to tell you the exact CSS you’d need.

    If you want to try yourself, here are 2 changes that might work:

    1. display_tag="span"
    2. Add the following CSS in your Appearance?>?Customize?>?Additional CSS. .wporg-box { display: inline !important; }

    You can also replace wporg-box with any other class you want and even add a custom CSS class to your display tag in the code.

    Example:

    $o = '<div class="my-cool-css-class">';

    $o .= '<' . $t . ' class="my-other-cool-css-class">' . esc_html__( $current_user->user_firstname ) . ''

    The beauty (and ugliness) of CSS is there are (too) many ways you can style your content. The good news is there are tons of docs on CSS (and of course AI chatbots) ??

    Good luck.

    thanks, I managed to do it with php I think by changing the 4th last line to include “welcome, “

    $o .= '<' . $t . '>' . 'Welcome, ' . esc_html__( $current_user->user_firstname ) . '';

    seems to work, many previous attempts just broke my site

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Shortcode for displaying the user’s first name?’ is closed to new replies.