1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Display users name in header for Wordpress

Discussion in 'PHP' started by bellcom, Jan 10, 2015.

  1. #1
    I am not sure which category to ask this question, wordpress, javascript or php?
    I have a shortcode that I use on wordpress pages that displays the author or site owners first & last name. That works on the pages but I am trying to get something to work in the template files so I can display the same info in the header.php.

    I can come close and know how to display the info for the current_user or logged in user but I need it to display for everyone and use the meta data from the post author instead.

    If this is possible I can post the code I am using now or should this be in another section on the forum?

    thanks!
     
    bellcom, Jan 10, 2015 IP
  2. CodeShop

    CodeShop Active Member

    Messages:
    137
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Hi,
    You can use plugin's shortcode in wordpress templates. You have to just call do_shortcodes function in the header file. For further documentation. Here is the link. http://codex.wordpress.org/Function_Reference/do_shortcode
     
    CodeShop, Jan 11, 2015 IP
  3. bellcom

    bellcom Well-Known Member

    Messages:
    218
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Yeah but the shortcode function I am using is 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.
    I am 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.

    This is the shortcode I used which works on a page or post but not in the header.php

    add_shortcode( 'user_name' , 'z_get_current_name' );
    function z_get_current_name( $atts ){
    $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>';
    }

    It will work using do_shortcode like you mentioned if I add
    $user_id = get_current_user_id();

    But then only displays the name if your logged in so it can get the user id, I need it to display the info from the post author.
    Make sense?
     
    bellcom, Jan 11, 2015 IP
  4. bellcom

    bellcom Well-Known Member

    Messages:
    218
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Was trying to edit op but it bumped, found the solution, problem [solved]
     
    bellcom, Jan 11, 2015 IP
  5. CodeShop

    CodeShop Active Member

    Messages:
    137
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #5
    It is great. This is also essential that you should use right function to get details from the wordpress database. Usually, the main problem is loading speed of the website decrease, when you do it incorrectly. It is good that you have found the solution. Keep an eye on speed optimization.
    <?php the_author(); ?> You can use this function to display author of a post. Additional parameter is post id. You can pass within this function. Optimize speed and improve usage of functions will result in healthier website.
     
    CodeShop, Jan 12, 2015 IP
  6. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #6
    does wordpress allow PHP code so your header would only need to be
    $_SESSION['first_name'] = $_POST['first_name'];
    $_SESSION['last_name'] = $_POST['last_name'];

    if($_SESSION['last_name']){
    echo "Hello " . $_SESSION['first_name'] . " " . $_SESSION['last_name'];
    }
     
    ezprint2008, Jan 12, 2015 IP
  7. bellcom

    bellcom Well-Known Member

    Messages:
    218
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    120
    #7
    This is what I used since each site only has one admin/user/post_author:

    function z_show_current_name(){
    // Get the current admin user, using it's registered email as key
    $user_info = get_user_by( 'email', get_option('admin_email'));
    // Use the user_info found to distill the info wanted
    $user_fname = $user_info->first_name;
    $user_lname = $user_info->last_name;
    // Return the output to show
    return '<span class="lmn_user_info lmn_user_fname">' . $user_fname . '</span><span class="lmn_user_lname"> ' . $user_lname . '</span>';
    }

    then this in the template:
    <?php echo z_show_current_name(); ?>
     
    bellcom, Jan 12, 2015 IP
  8. CodeShop

    CodeShop Active Member

    Messages:
    137
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #8
    unset($user_info); after assigning values to variables and just before return.
     
    CodeShop, Jan 12, 2015 IP
  9. jesmin01

    jesmin01 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    i have also a question,how to install wordpress my local server?
    most of the time i spent for install warm server,but i can't successfully install it,
    my have a website related free wordpress theme
     
    jesmin01, Jan 26, 2015 IP
  10. Stepin Solutions

    Stepin Solutions Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #10
    See Function_Reference/get_currentuserinfo

    Make sense?
    http://www.stepin-solutions.com/
     
    Stepin Solutions, Feb 8, 2015 IP
  11. sarikabtech

    sarikabtech Well-Known Member

    Messages:
    186
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    108
    #11
    you can atch this video and follow steps to install wamp server

    i hope it will help you .

    Thanks!
     
    sarikabtech, Feb 16, 2015 IP