本文介绍了如何从 WP_User 对象获取 Wordpress 用户的名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基本的插件.这是我的代码:

I'm writing a basic plugin. Here's my code:

    $new_user = get_userdata($user_id); //$user_id is passed as a parameter

$first_name1 = $new_user->user_firstname;
$last_name1 = $new_user->user_lastname;
    echo "<" . $first_name1 . $last_name1 . ">";
    //returns: <>

$first_name2 = $new_user-first_name;
$last_name2 = $new_user->last_name;
    echo "<" . $first_name2 . $last_name2 . ">";
    //returns: <>

根据代码,这应该有效,但是当我回显 $first_name$last_name 它们是空的.奇怪的是,这个确实有效:

According to the codex, this should work, but when I echo $first_name or $last_name they're empty. Strangely, THIS does work:

    $id = $new_user->ID;

我做错了什么吗?

更新:

var_dump 编辑了 $new_user 并且这些属性不在那里!这是因为我从/mu-plugins 目录中的插件内部调用它吗?以后会添加这些属性吗?

I var_dumped $new_user and those properties aren't in there! Is this because I'm calling it from inside a plugin in the /mu-plugins directory? Do those properties get added later?

object(WP_User)#334 (7) { ["data"]=> object(stdClass)#330 (10) { ["ID"]=> string(3) "758" ["user_login"]=> string(7) "emerson" ["user_pass"]=> string(34) "$P$BB2PuvRbyGUSVZR1M8FLSujPvMO2MW0" ["user_nicename"]=> string(7) "emerson" ["user_email"]=> string(16) "[email protected]" ["user_url"]=> string(0) "" ["user_registered"]=> string(19) "2012-08-17 01:03:27" ["user_activation_key"]=> string(0) "" ["user_status"]=> string(1) "0" ["display_name"]=> string(7) "emerson" } ["ID"]=> int(758) ["caps"]=> array(1) { ["subscriber"]=> string(1) "1" } ["cap_key"]=> string(15) "wp_capabilities" ["roles"]=> array(1) { [0]=> string(10) "subscriber" } ["allcaps"]=> array(15) { ["read"]=> bool(true) ["level_0"]=> bool(true) ["read_questions"]=> bool(true) ["read_answers"]=> bool(true) ["publish_questions"]=> bool(true) ["immediately_publish_questions"]=> bool(true) ["publish_answers"]=> bool(true) ["read_private_forums"]=> bool(true) ["publish_topics"]=> bool(true) ["edit_topics"]=> bool(true) ["publish_replies"]=> bool(true) ["edit_replies"]=> bool(true) ["assign_topic_tags"]=> bool(true) ["access_s2member_level0"]=> bool(true) ["subscriber"]=> string(1) "1" } ["filter"]=> NULL }

更新2:

我试过了:

$user_meta = get_user_meta( $new_user->ID );
var_dump($user_meta);

我得到了这个(last_name 和 first_name 是空的,即使它们是在用户的配置文件中定义的):

and I got this (last_name and first_name are empty even though they're defined in the user's profile):

array(11) { ["wp_user_level"]=> array(1) { [0]=> string(1) "0" } ["show_admin_bar_front"]=> array(1) { [0]=> string(4) "true" } ["wp_capabilities"]=> array(1) { [0]=> string(32) "a:1:{s:10:"subscriber";s:1:"1";}" } ["use_ssl"]=> array(1) { [0]=> string(1) "0" } ["admin_color"]=> array(1) { [0]=> string(5) "fresh" } ["comment_shortcuts"]=> array(1) { [0]=> string(5) "false" } ["rich_editing"]=> array(1) { [0]=> string(4) "true" } ["description"]=> array(1) { [0]=> string(0) "" } ["nickname"]=> array(1) { [0]=> string(7) "emerson" } ["last_name"]=> array(1) { [0]=> string(0) "" } ["first_name"]=> array(1) { [0]=> string(0) "" } }

推荐答案

用户的名字和姓氏存储在 user_meta 中.

The user's first and last names are stored within the user_meta.

http://codex.wordpress.org/Function_Reference/get_user_meta

例如,如果您想访问所有这些数据,请尝试以下操作:

So if you'd like to access all of that data, for example, try this:

$user_meta = get_user_meta( $new_user->ID );

或者,如果您想访问单个元值,请尝试以下操作:

Or, if you'd like to access a single meta value, try this:

$last_name = get_user_meta( $new_user->ID, 'last_name', true );

这篇关于如何从 WP_User 对象获取 Wordpress 用户的名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:34