Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get current logged in username in the functions.php file WordPress

  • SOLVED

I am wanting to get the current logged in username to be used through out my functions.php file.

I have this in there right now and it will not display the user name:

global $current_user;
get_currentuserinfo();

echo 'Current User: ' . $current_user->user_login;



UPDATED functions.php file:

<?

global $current_user;
get_currentuserinfo();

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
function my_custom_dashboard_widgets() {
global $wp_meta_boxes;
wp_add_dashboard_widget('custom_help_widget', 'General Info', 'custom_dashboard_help');
}


function custom_dashboard_help() {
echo 'Current User: ' . $current_user->user_login;
}

Answers (5)

2012-07-18

Arnav Joy answers:

try this in functions.php
<?php
function get_user_log_in_or_not(){
if(is_user_logged_in()) {
global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login ;
}
else
echo 'User not logged in';

}
?>


call this as : <?php get_user_log_in_or_not();?>


Arnav Joy comments:

try this



<?







add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');

function my_custom_dashboard_widgets() {

global $wp_meta_boxes;

wp_add_dashboard_widget('custom_help_widget', 'General Info', 'custom_dashboard_help');

}





function custom_dashboard_help() {

global $current_user;

get_currentuserinfo();
echo 'Current User: ' . $current_user->user_login;

}


69developer comments:

That worked, need the global added in that function. Thanks!

2012-07-18

Francisco Javier Carazo Gil answers:

What's the problem? Function does not exist?

Try this:

require (ABSPATH . WPINC . '/pluggable.php');
get_currentuserinfo();


69developer comments:

The problem is in the functions.php file where I have the above code, the <strong>$current_user->user</strong>_login is blank.


So your saying I need your 'require' code above in the functions.php file, in order to be able to access teh $current_user properties?


Francisco Javier Carazo Gil comments:

If you don't have a problem about "function does not exists" this is not the problem. I'm going to see it.


Francisco Javier Carazo Gil comments:

Try this one: http://codex.wordpress.org/Function_Reference/wp_get_current_user


69developer comments:

See my origianl post, updated code section. The $current_user properties are all blank.

2012-07-18

Sabby Sam answers:

Hi,

use like

function get_current_userlogged()
{
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";
}

Call this function out site the wordpress
like
<?php
get_current_userlogged();
?>


Sabby Sam comments:

I have checked and it is working try this code.


69developer comments:

See my origianl post, updated code section. The $current_user properties are all blank.


Sabby Sam comments:


<?



global $current_user;

get_currentuserinfo();



add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');

function my_custom_dashboard_widgets() {

global $wp_meta_boxes;

wp_add_dashboard_widget('custom_help_widget', 'General Info', 'custom_dashboard_help');

}





function custom_dashboard_help() {

get_current_userlogged();

}

function get_current_userlogged()
{
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";
}

2012-07-18

Dbranes answers:

Your code-snippet works in my wordpress 3.4.1

Do you want the display name?

Have you tried to view the HTML source of your webpage and search for 'Current User: '.?



69developer comments:

See my origianl post, updated code section. The $current_user properties are all blank.


Dbranes comments:

you just placed

global $current_user;


in a wrong place, should be inside the "custom_dashboard_help" function

I can see that Arnav Joy has already posted the correct solution

2012-07-18

isimpkinreita answers:

Sanqi Bikini Swimsuit with Cover Up - SQ13036 [SQ13036] - $59.00 :