您的代码中至少有三个错误。第一在尝试更新用户信息之前,您正在“返回”,因此wp_update_user()
未执行函数。其次,您对用户ID进行了硬编码,因此您总是试图修改ID=1的用户的昵称
return $tableuserinf;
$user_id = 1;
$nicksname = $_POST[\'fepusernick\'];
wp_update_user( array ( \'ID\' => $user_id, \'nickname\' => $nicksname ) ) ;
应为:
$user_id = $current_user->ID;
$nicksname = $_POST[\'fepusernick\'];
wp_update_user( array ( \'ID\' => $user_id, \'nickname\' => $nicksname ) ) ;
return $tableuserinf;
您可以进一步进行一些检查:
$user_id = $current_user->ID;
// Not sure if sanitize_user() is needed or is executed by wp_update_user(). Anyway, it doesn\'t hurt
$nicksname = isset($_POST[\'fepusernick\']) ? sanitize_user($_POST[\'fepusernick\']) : \'\';
//check if new nickname is different before performing the update operation
if($nicksname != $current_user->nickname){
wp_update_user( array ( \'ID\' => $user_id, \'nickname\' => $nicksname ) ) ;
}
return $tableuserinf;