WordPress中的这些代码块之间有什么区别


What is the difference between these blocks of code in WordPress?

这不起作用:

$simple_local_avatars = new Simple_Local_Avatars;
if(isset($_POST['save_account_details'] ) ) {
    $user_id = get_current_user_id();
    $simple_local_avatars->edit_user_profile_update($user_id);
}

但这正在起作用,只是没有那么动态:

$simple_local_avatars = new Simple_Local_Avatars;
if(isset($_POST['save_account_details'] ) ) {
    $simple_local_avatars->edit_user_profile_update(58);
}

edit_user_profile_update函数需要当前登录的用户 ID。

您的第一个代码块无法检索用户 ID,而您的第二个代码块可以。尝试echo第一个中的$user_id,它将是空的。

换句话说:get_current_user_id();什么也不返回。它是空的。

编辑:

if(isset($_POST['save_account_details'] ) ) {
    $user_id = get_current_user_id();
    echo $user_id;
    $simple_local_avatars->edit_user_profile_update($user_id);
}

目前似乎没有用户登录,这就是您获得0作为返回用户 ID 的原因。

因此,请尝试类似操作以确保用户是否已登录:

编辑:

    if(isset($_POST['save_account_details'] ) ) {
        global $current_user;
        get_currentuserinfo();
        $user_id = $current_user->ID;
         if($user_id){
           $simple_local_avatars->edit_user_profile_update($user_id);
         }else{
                wp_die("Please log in first.");
         }
}

希望对您有所帮助..