New Landing How can we help? Atelier Use php to call username

Viewing 10 posts - 1 through 10 (of 10 total)
  • Posted in: Atelier
  • #218052
    NMillard
    Member
    Post count: 115

    I’m trying to make a “Hi [Username]” thing on my front page – but I just can’t get the name to show.

    I am using this code:

    <?php
    $current_user = wp_get_current_user();

    echo ‘username:’ . $current_user->user_login . ‘<br />’;

    ?>

    but nothing works… how would you do it? 🙂

    #218055
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Hi,

    it should be something like this.

    <?php global $current_user;
          get_currentuserinfo();
    
          echo 'Username: ' . $current_user->user_login . "\n";
          echo 'User email: ' . $current_user->user_email . "\n";
          echo 'User level: ' . $current_user->user_level . "\n";
          echo 'User first name: ' . $current_user->user_firstname . "\n";
          echo 'User last name: ' . $current_user->user_lastname . "\n";
          echo 'User display name: ' . $current_user->display_name . "\n";
          echo 'User ID: ' . $current_user->ID . "\n";
    ?>

    give it a try.

    -Rui

    #218060
    NMillard
    Member
    Post count: 115

    I’ve also tried that – it just prints out the code.

    Should I use any specific element? I’ve tried with text block and RAW HTML..

    #218064
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    That’s different, that code only runs in php.

    Try to use a plugin like this one
    https://wordpress.org/plugins/php-code-for-posts/

    You create the snippet of php and you will have a shortcode that you can paste inside of our Text Block assets so it could render the shortcode.

    -Rui

    #218085
    NMillard
    Member
    Post count: 115

    Ah okay, cool! Thanks!

    #218087
    Kyle – SUPPORT
    Moderator
    Post count: 35880

    No problem, thanks Rui

    #231958
    pedroandrade
    Member
    Post count: 31

    HI i’d like to add this greeting like “Welcome ‘username’ ” on the header, using php snippet and shortcode.
    I’ve already make it work But how can make it only visible when the user is logged in?
    Because when the user is not logged in, it only prints ” Welcome ” and it makes no sense.
    Thank you

    #232060
    Mohammad – SUPPORT
    Moderator
    Post count: 27441

    Hi,
    Please paste this code at functions.php of child theme:-

     function sf_woo_links( $position = "", $config = "" ) {
    
                // VARIABLES
                global $sf_options;
    
                $tb_search_text   = $sf_options['tb_search_text'];
                $woo_links_output = $ss_enable = "";
    			$supersearch_icon = apply_filters('sf_header_supersearch_icon', '<i class="ss-zoomin"></i>');
    
                if ( isset( $sf_options['ss_enable'] ) ) {
                    $ss_enable = $sf_options['ss_enable'];
                } else {
                    $ss_enable = true;
                }
    
                // WOO LINKS OUTPUT
                $woo_links_output .= '<nav class="' . $position . '">' . "\n";
                $woo_links_output .= '<ul class="menu">' . "\n";
                if ( is_user_logged_in() ) {
                    global $current_user;
                    get_currentuserinfo();
                    $woo_links_output .= '<li class="tb-welcome">' . __( "Welcome", "swiftframework" ) . " " . $current_user->display_name . '</li>' . "\n";
                } 
                if ( $ss_enable ) {
                    if ( $position == "top-menu" ) {
                        $woo_links_output .= '<li class="tb-woo-custom clearfix"><a class="swift-search-link" href="#">'.$supersearch_icon.'<span>' . do_shortcode( $tb_search_text ) . '</span></a></li>' . "\n";
                    } else {
                        $woo_links_output .= '<li class="hs-woo-custom clearfix"><a class="swift-search-link" href="#">'.$supersearch_icon.'<span>' . do_shortcode( $tb_search_text ) . '</span></a></li>' . "\n";
                    }
                }
                $woo_links_output .= '</ul>' . "\n";
                $woo_links_output .= '</nav>' . "\n";
    
                // RETURN
                return $woo_links_output;
            }

    Thanks
    Mohammad

    #232551
    pedroandrade
    Member
    Post count: 31

    H Mohammad, i’ve pasted that code on functions.php, but it breaks all the website and basicaly it prints out the code on the top…
    Can you help out?
    Regards

    #232597
    Mohammad – SUPPORT
    Moderator
    Post count: 27441

    Hi,
    Just remove the last code and use this code:-

    <?php
    function sf_woo_links( $position = “”, $config = “” ) {

    // VARIABLES
    global $sf_options;

    $tb_search_text = $sf_options[‘tb_search_text’];
    $woo_links_output = $ss_enable = “”;
    $supersearch_icon = apply_filters(‘sf_header_supersearch_icon’, ‘<i class=”ss-zoomin”></i>’);

    if ( isset( $sf_options[‘ss_enable’] ) ) {
    $ss_enable = $sf_options[‘ss_enable’];
    } else {
    $ss_enable = true;
    }

    // WOO LINKS OUTPUT
    $woo_links_output .= ‘<nav class=”‘ . $position . ‘”>’ . “\n”;
    $woo_links_output .= ‘<ul class=”menu”>’ . “\n”;
    if ( is_user_logged_in() ) {
    global $current_user;
    get_currentuserinfo();
    $woo_links_output .= ‘<li class=”tb-welcome”>’ . __( “Welcome”, “swiftframework” ) . ” ” . $current_user->display_name . ‘‘ . “\n”;
    }
    if ( $ss_enable ) {
    if ( $position == “top-menu” ) {
    $woo_links_output .= ‘<li class=”tb-woo-custom clearfix”>‘.$supersearch_icon.'<span>’ . do_shortcode( $tb_search_text ) . ‘</span>‘ . “\n”;
    } else {
    $woo_links_output .= ‘<li class=”hs-woo-custom clearfix”>‘.$supersearch_icon.'<span>’ . do_shortcode( $tb_search_text ) . ‘</span>‘ . “\n”;
    }
    }
    $woo_links_output .= ‘‘ . “\n”;
    $woo_links_output .= ‘</nav>’ . “\n”;

    // RETURN
    return $woo_links_output;
    }
    ?>

    Thanks
    Mohammad

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in and have valid license to reply to this topic.

License required for one of the following items
Login and Registration Log in · Register