theme development - WordPress wp_get_current_user returning blank values until refresh

I'm developing a site in WordPress and creating my own theme.As part of the theme, I have a custom user login.W

I'm developing a site in WordPress and creating my own theme. As part of the theme, I have a custom user login. When a user logs in, the form action directs back to itself and in the header of the page, I have a simple script which determines if the user is logged in or not.

While this script runs as expected, the issue I encounter is trying to load the user's first and last name with the use of wp_get_current_user function, the name returned is blank on the initial load (although it does show once the page is refreshed or when migrating to a new page).

I've searched several user questions and articles and many of them mention hooking into the init function (such as this one), but none of them provide additional detail on this. I've attempted to add a filter to the init function, but have had no luck.

Here's my code to handle the login, located at the start of the header.php file:

<?php
if (is_user_logged_in()) {
    $userLoggedIn = true;
    if (isset($_POST['spLogout'])) {
        wp_logout();
        $userLoggedIn = false;
    }
} else {
    $userLoggedIn = false;
    if (isset($_POST['spLogin'])) {
        $ax_user = $_POST['email'];
        $ax_pswd = $_POST['pswd'];
        $creds = array(
            'user_login'    => $ax_user,
            'user_password' => $ax_pswd,
            'remember'      => true
        );

        $user = wp_signon( $creds, false);

        if (is_wp_error($user)) {
            echo $user->get_error_message();
        } else {
            $userLoggedIn = true;
        }
    }
}

?>

And in my functions.php file, here is the hook and function:

add_filter('init', 'ax_get_user_info');
function avox_get_user_info() {
    if (is_user_logged_in()) {
        $currentUser = wp_get_current_user();
        $fName = $currentUser->user_firstname;
    }
    return $fName;
}

I'd like to be able to load user information without requiring a page refresh. Perhaps I'm doing this wrong, I'm only just starting to learn about hooks and loading order within WordPress, but any advice is greatly appreciated.

I'm developing a site in WordPress and creating my own theme. As part of the theme, I have a custom user login. When a user logs in, the form action directs back to itself and in the header of the page, I have a simple script which determines if the user is logged in or not.

While this script runs as expected, the issue I encounter is trying to load the user's first and last name with the use of wp_get_current_user function, the name returned is blank on the initial load (although it does show once the page is refreshed or when migrating to a new page).

I've searched several user questions and articles and many of them mention hooking into the init function (such as this one), but none of them provide additional detail on this. I've attempted to add a filter to the init function, but have had no luck.

Here's my code to handle the login, located at the start of the header.php file:

<?php
if (is_user_logged_in()) {
    $userLoggedIn = true;
    if (isset($_POST['spLogout'])) {
        wp_logout();
        $userLoggedIn = false;
    }
} else {
    $userLoggedIn = false;
    if (isset($_POST['spLogin'])) {
        $ax_user = $_POST['email'];
        $ax_pswd = $_POST['pswd'];
        $creds = array(
            'user_login'    => $ax_user,
            'user_password' => $ax_pswd,
            'remember'      => true
        );

        $user = wp_signon( $creds, false);

        if (is_wp_error($user)) {
            echo $user->get_error_message();
        } else {
            $userLoggedIn = true;
        }
    }
}

?>

And in my functions.php file, here is the hook and function:

add_filter('init', 'ax_get_user_info');
function avox_get_user_info() {
    if (is_user_logged_in()) {
        $currentUser = wp_get_current_user();
        $fName = $currentUser->user_firstname;
    }
    return $fName;
}

I'd like to be able to load user information without requiring a page refresh. Perhaps I'm doing this wrong, I'm only just starting to learn about hooks and loading order within WordPress, but any advice is greatly appreciated.

Share Improve this question asked Jun 24, 2019 at 14:15 XthrallsXthralls 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Your header.php file is going to be called after the init action. If you want the logic to run in your header.php file first, maybe you can hook into another action done at a later point in WordPress:

add_action('wp_head', 'ax_get_user_info');
function avox_get_user_info() {
    if (is_user_logged_in()) {
        $currentUser = wp_get_current_user();
        $fName = $currentUser->user_firstname;
    }
    return $fName;
}

init is done very early on, while wp_head is called in your header.php file most likely after your other logic is being fired. You can see here init is called before get_header, which is calling your header.php file.

https://codex.wordpress/Plugin_API/Action_Reference

https://codex.wordpress/Plugin_API/Action_Reference/wp_head

Hope that helps!!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745374518a4624941.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信