How to display loggedin user name in header - wordpress - wordpress

I was new to WordPress.
Need help to display logged in user name in place of "My Account" in wordpress. Like "Hi, UserName".
Im using woocommerce plugin as my website is related to e-commerce.
Kindly help with detailed steps to solve this query.

From the WP Codex (https://codex.wordpress.org/Function_Reference/wp_get_current_user):
The call to wp_get_current_user() always returns a WP_User object.
<?php
$current_user = wp_get_current_user();
/**
* #example Safe usage:
* $current_user = wp_get_current_user();
* if ( ! $current_user->exists() ) {
* return;
* }
*/
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
?>

You might want to check if the current user is a logged in user.
<?php
// Check if user is logged in
if ( is_user_logged_in() ) {
// Get current logged in user details
$current_user = wp_get_current_user();
echo $current_user->display_name;
} else {
echo 'Not logged in';
}
?>

Related

Showing User Details and Shipping Address on account dashboard

I would like to be able to show more user details than I'm able to figure out in the woocommerce account dashboard. Details being name and email fields.
Also would like to add shipping details aswell?
My details answer
<?php
$current_user = wp_get_current_user();
/**
* #example Safe usage: $current_user = wp_get_current_user();
* if ( !($current_user instanceof WP_User) )
* return;
*/
echo 'Email:' . $current_user->user_email . '<br />';
echo 'First Name:' . $current_user->user_firstname . '<br />';
echo 'Last Name:' . $current_user->user_lastname . '<br />';
?>
Address add to functions.php
add_action( 'woocommerce_account__shipping_dashboard', 'woocommerce_account_edit_address' );
Add to dashboard to show address
<?php do_action( 'woocommerce_account__shipping_dashboard' ); ?>

Insert custom content in WooCommerce

I have some problem in WooCommerce Admin Order menu. I want to add some content in the column but I don't know where I can make it.
This is some example screenshoot about my woocommerce admin order view :
I want to add phone number bellow email address in Order column.
Please help me to do that.
Thank you.
I've had to do this once, i've followed this tutorial :
https://code.tutsplus.com/articles/add-a-custom-column-in-posts-and-custom-post-types-admin-screen--wp-24934
add_action('manage_shop_order_posts_custom_column', 'match_order_woocommerce_custom_order_columns', 2);
function match_order_woocommerce_custom_order_columns( $column ) {
remove_action('manage_shop_order_posts_custom_column', 'woocommerce_custom_order_columns', 2);
global $post, $woocommerce;
$order = new WC_Order( $post->ID );
switch ($column) {
case "order_title" :
if ($order->user_id) $user_info = get_userdata($order->user_id);
if (isset($user_info) && $user_info) :
$user = '<a href="user-edit.php?user_id=' . esc_attr( $user_info->ID ) . '">';
if ($user_info->first_name || $user_info->last_name) $user .= $user_info->first_name.' '.$user_info->last_name;
else $user .= esc_html( $user_info->display_name );
$user .= '</a>';
else :
$user = __('Guest', 'woocommerce');
endif;
echo '<strong>'.sprintf( __('Order %s', 'woocommerce'), $order->get_order_number() ).'</strong> ' . __('made by', 'woocommerce') . ' ' . $user;
if ($order->billing_email) :
echo '<small class="meta">'.__('Email:', 'woocommerce') . ' ' . ''.esc_html( $order->billing_email ).'</small>';
endif;
if ($order->billing_phone) :
echo '<small class="meta">'.__('Tel:', 'woocommerce') . ' ' . esc_html( $order->billing_phone ) . '</small>';
endif;
break;
}
}
Please try this snippet in your active theme's functions.php

wordpress woocommerce custom login with error processing

I'm currently designing a site with a custom dropdown login, see here:
http://tens-ecigs.co.uk/test/
The login script wasn't developed by me and I suspect it isn't entirely correct, looking at it it's supposed to have error processing however when I mistype a username or password it takes me to the standard wordpress login page and processes the error there.
Can anyone point me in the right direction in terms of proessing the error on the dropdown instead of re-directing to the standard wordpress login? This is the current code:
<fieldset id="signin_menu">
<?php if (is_user_logged_in()) {
$user = get_user_by('id', get_current_user_id());
if ( $logged_in_title ) echo $before_title . sprintf( $logged_in_title, ucwords($user->display_name) ) . $after_title;
do_action('woocommerce_login_widget_logged_in_before_links');
$links = apply_filters( 'woocommerce_login_widget_logged_in_links', array(
__('My account', 'woocommerce') => get_permalink(woocommerce_get_page_id('myaccount')),
__('Change my password', 'woocommerce') => get_permalink(woocommerce_get_page_id('change_password')),
__('Logout', 'woocommerce') => wp_logout_url(home_url())
));
if (sizeof($links>0)) :
echo '<div style="padding-left:10px">';
foreach ($links as $name => $link) :
echo ''.$name.'<br>';
endforeach;
echo '</div>';
endif;
do_action('woocommerce_login_widget_logged_in_after_links');
} else {
if ( $logged_out_title ) echo $before_title . $logged_out_title . $after_title;
do_action('woocommerce_login_widget_logged_out_before_form');
global $login_errors;
if ( is_wp_error($login_errors) && $login_errors->get_error_code() ) foreach ($login_errors->get_error_messages() as $error) :
echo '<div class="woocommerce_error">' . $error . "</div>\n";
break;
endforeach;
// Get redirect URL
$redirect_to = apply_filters( 'woocommerce_login_widget_redirect', get_permalink(woocommerce_get_page_id('myaccount')) );
} ?>
/test/wp-login.php" method="post">
Username
Password
Remember me
" id=forgot_username_link
title="If you don't remember your password you can retrive it here" >Forgot your password?

how to show user's username when logged into a wordpress site?

I would like to have the header of my site reading "Logged in as " when a user is logged into my wordpress site.
I dont know how to echo the username of the current user, heres the code I'm got...
<?php
if ( is_user_logged_in() ) {
echo '<ul id="mem" class="clearfix">
<li>Logged in as Username</li>
<li>Logout</li>
</ul>';
} else {
echo '<ul id="mem" class="clearfix">
<li>Register</li>
<li>Login</li>
</ul>';
}
?>
and I'd like to replace Username.
Any help is much appreciated.
Thanks,
Mark
you can use get_currentuserinfo() function to grab any logged in users info..
<?php global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User email: ' . $current_user->user_email . "\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";
?>
in your case it could be
<?php
if ( is_user_logged_in() ) {
global $current_user;
get_currentuserinfo();
echo '<ul id="mem" class="clearfix">
<li>Logged in as '.$current_user->display_name.'</li>
<li>Logout</li>
</ul>';
} else {
echo '<ul id="mem" class="clearfix">
<li>Register</li>
<li>Login</li>
</ul>';
}
?>
:)
$current_user = wp_get_current_user();
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';

At what point during the Wordpress Lifecycle can I use wp_get_current_user?

I'm writing an integration piece with a third party system and need to check certain values when a user logs in.
I've tried several different hooks: wp_init, wp_login and wp_loaded, but wp_get_current_user always returns null.
E.g.: To determine if there is a user currently logged in, do this:
<?php
wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
} else {
// Logged in.
}
?>
default usage
The call to wp_get_current_user() return WP_User object.
<?php
wp_get_current_user();
/**
* #example Safe usage: $current_user = wp_get_current_user();
* if ( !($current_user instanceof WP_User) )
* return;
*/
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User level: ' . $current_user->user_level . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
?>
more on this page http://codex.wordpress.org/Function_Reference/wp_get_current_user
I was able to accomplish this using http://codex.wordpress.org/Function_Reference/get_user_by.
function CheckWordPressLogin($User) {
//$User contains login user name.
$CurrentUser = get_user_by('login', $User); //$CurrentUser is a WP_User object.
}
add_action('wp_login', 'CheckWordPressLogin');

Resources