4

I used following code

function userinfo_global() {
    global $users_info;
    wp_get_current_user();
}
add_action( 'init', 'userinfo_global' );

in a file users.php , this file are call in inside funtions.php.

in template file I have <?php echo $users_info->user_firstname; ?> , but no working..

I want to do global wp_get_current_user();

You know why?

5

You'll also have to fill the variable, e.g.

function userinfo_global() {
    global $users_info;
    $users_info = wp_get_current_user();
}
add_action( 'init', 'userinfo_global' );

And you should then be able to use $users_info everywhere in global context. Keep in mind that some template pars (header.php, footer.php, those used via get_template_part) are not in global scope by default, so you'll have to use global $users_info; in those files before accessing the variable.

4

wp_get_current_user() is already a global function predefined by WordPress. Since you'd have to add global $users_info; to your templates to make sure that you always have the global variable, you might just as well have $users_info = wp_get_current_user(); in its place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.