Tuesday, November 10, 2020

How to get Logged In Username in Wordpress or Woocommerce

It is really easy to get the logged-in user’s information in Wordpress or Woocommerce. In this article we will show you how to get the related to the currently logged in user.

We will utilize get_currentuserinfo(); function. This could be used anywhere in your theme (header, footer, sidebar, page-template etc). In order for this to work this code the user must be logged-in.


/*to get username*/


function show_loggedin_function( $atts ) {

global $current_user, $user_login;
get_currentuserinfo();
add_filter('widget_text', 'do_shortcode');
if ($user_login)
return 'Hi ' . $current_user->display_name . '!';
else
return '<a href="' . wp_login_url() . ' ">Login</a>';

}
add_shortcode( 'show_loggedin_as', 'show_loggedin_function' );
Where ever you want to show the logged in Username just call the above function

No comments:

Post a Comment

How to get Logged In Username in Wordpress or Woocommerce

It is really easy to get the logged-in user’s information in Wordpress or Woocommerce. In this article we will show you how to get the relat...