Using shortcode in header.php template

I have a shortcode that works on my pages, [user_name] and it displays the site owners first & last name. I would like to display that info in my header template so tried do_shortcode but I don't know what I am doing :slight_smile:

<?php
echo do_shortcode( '[user_name]' );
?>

Maybe someone could correct if possible?
Thanks!!!

Here is code from my shortcode plugin if this helps:

add_shortcode( 'user_name' , 'z_get_current_name' );
function z_get_current_name( $atts ){
$user_fname = get_the_author_meta('user_firstname');
$user_lname = get_the_author_meta('user_lastname');
extract( shortcode_atts( array(
'text' => null,
), $atts ) );
return '<span class="lmn_user_info lmn_user_fname">' . $text . '' . $user_fname . '</span><span class="lmn_user_lname"> ' . $user_lname . '</span>';
}

  • Vinod Dalvi

    Hi George,

    I hope you are well today and thank you for your question.

    In your shortcode function you are using the WordPress function get_the_author_meta.

    This function returns the desired meta data for a user. If used within The Loop, the user ID need not be specified, and the displayed data is that of the current post author. A user ID must be specified if used outside The Loop.

    You are using it in the header.php template file which most of the time doesn't contain loop so you must specify user ID to the get_the_author_meta function.

    To do this try using the following shortcode code replacing the code in your shortcode plugin.

    add_shortcode( 'user_name' , 'z_get_current_name' );
    function z_get_current_name( $atts ){
    $user_id = get_current_user_id();
    $user_fname = get_the_author_meta('user_firstname', $user_id);
    $user_lname = get_the_author_meta('user_lastname', $user_id);
    extract( shortcode_atts( array(
    'text' => null,
    ), $atts ) );
    return '<span class="lmn_user_info lmn_user_fname">' . $text . '' . $user_fname . '</span><span class="lmn_user_lname"> ' . $user_lname . '</span>';
    }

    I have tested the above solution on my site and it's working fine.

    Best Regards,
    Vinod Dalvi

  • George

    Thanks Vinod !
    This does work IF I am logged in, so it shows the current user info but is blank if not logged in and I want it to be visible to all and show the site owners name & avatar in the header and NOT the logged in users info.

    I think I may have left some info out since I see this in my shortcode file so I am attaching all of the code in a txt file.

    Can we make it display all the time even if not logged in as it does if used in the page content area?

    I updated the shortcode plugin with your code and if I use the shortcode on a page it still works even when not logged in.

    I added the following to header and here is the page:
    http://listmenow.com/evantage/

    <div class="p-image">
    <?php
        global $current_user;
    
            get_currentuserinfo();
    
            echo get_avatar( $current_user->ID, 128 );
     ?>
    
    </div>
    <div class="u-name">
    
    <?php
    echo do_shortcode('[user_name]');
    ?>
    
      </div>

    I was hoping you could also help with my code to display the site owners Avatar :slight_smile: It also shows only for the logged in user but NOT the site owner avatar at all times.

    Thanks for all your help today, much appreciated!!!

  • Vinod Dalvi

    Hi George,

    Thank you for your reply.

    To achieve it try using the following modified code of shortcode

    add_shortcode( 'user_name' , 'z_get_current_name' );
    function z_get_current_name( $atts ){
    	$admin_email = get_option( 'admin_email' );
    	$user = get_user_by( 'email', $admin_email );
    	if( $user ){
    		$user_id = $user->data->ID;
    		$user_fname = get_the_author_meta('user_firstname', $user_id);
    		$user_lname = get_the_author_meta('user_lastname', $user_id);
    		extract( shortcode_atts( array(
    		'text' => null,
    		), $atts ) );
    		return '<span class="lmn_user_info lmn_user_fname">' . $text . '' . $user_fname . '</span><span class="lmn_user_lname"> ' . $user_lname . '</span>';
    	}
    
    	return '';
    }

    And add the following code in the header.

    <div class="p-image">
    	<?php
    		$admin_email = get_option( 'admin_email' );
    		$user = get_user_by( 'email', $admin_email );
    		if( $user ){
    			$user_id = $user->data->ID;
    	    	echo get_avatar( $user_id, 128 );
    	    }
    	 ?>
    
    </div>
    <div class="u-name">
    
    	<?php
    	echo do_shortcode('[user_name]');
    	?>
    
    </div>

    Best Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.