函数文档

get_profile()

💡 云策文档标注

概述

get_profile() 是一个已弃用的 WordPress 函数,用于基于指定字段检索用户数据。自 3.0.0 版本起,建议使用 get_the_author_meta() 替代。

关键要点

  • 函数已弃用:自 WordPress 3.0.0 起,get_profile() 被标记为弃用,应改用 get_the_author_meta()。
  • 功能描述:根据用户元字段检索用户数据,支持指定用户 ID 或默认当前用户。
  • 参数说明:$field(必需,字符串类型,用户元字段)和 $user(可选,整数或 false,用户 ID,默认为 false 表示当前用户)。
  • 返回值:返回字符串类型的作者字段数据。
  • 相关函数:涉及 get_user_by()、get_the_author_meta() 和 _deprecated_function() 等核心函数。

代码示例

function get_profile( $field, $user = false ) {
    _deprecated_function( __FUNCTION__, '3.0.0', 'get_the_author_meta()' );
    if ( $user ) {
        $user = get_user_by( 'login', $user );
        $user = $user->ID;
    }
    return get_the_author_meta( $field, $user );
}

注意事项

  • 弃用警告:使用此函数会触发 _deprecated_function(),建议在开发中避免使用,以保持代码兼容性和最佳实践。
  • 替代方案:直接调用 get_the_author_meta() 来实现相同功能,无需额外处理弃用逻辑。

📄 原文内容

Retrieve user data based on field.

Description

See also

Parameters

$fieldstringrequired
User meta field.
$userfalse|intoptional
User ID to retrieve the field for. Default false (current user).

Default:false

Return

string The author’s field from the current author’s DB object.

Source

function get_profile( $field, $user = false ) {
	_deprecated_function( __FUNCTION__, '3.0.0', 'get_the_author_meta()' );
	if ( $user ) {
		$user = get_user_by( 'login', $user );
		$user = $user->ID;
	}
	return get_the_author_meta( $field, $user );
}

Changelog

Version Description
3.0.0 Deprecated. Use get_the_author_meta()
1.5.0 Introduced.