Digital experiences for all disciplines
New Landing › How can we help? › Atelier › Use php to call username
New Landing › How can we help? › Atelier › Use php to call username
- This topic has 9 replies, 5 voices, and was last updated 8 years by Mohammad – SUPPORT.
-
Posted in: Atelier
-
October 2, 2015 at 11:04 am #218052
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? 🙂
October 2, 2015 at 11:08 am #218055Hi,
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
October 2, 2015 at 11:14 am #218060I’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..
October 2, 2015 at 11:27 am #218064That’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
October 2, 2015 at 12:02 pm #218085Ah okay, cool! Thanks!
October 2, 2015 at 12:04 pm #218087No problem, thanks Rui
November 28, 2015 at 2:25 pm #231958HI 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 youNovember 30, 2015 at 7:30 am #232060Hi,
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
MohammadDecember 1, 2015 at 7:42 pm #232551H 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?
RegardsDecember 2, 2015 at 8:56 am #232597Hi,
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 -
Posted in: Atelier
You must be logged in and have valid license to reply to this topic.