钩子文档

edit_user_profile

💡 云策文档标注

概述

edit_user_profile 是一个 WordPress 动作钩子,在‘编辑用户’屏幕的‘应用程序密码’部分加载后触发。它主要用于在用户资料页面底部输出自定义字段或数据,但仅当当前用户编辑其他用户的资料时生效。

关键要点

  • 触发时机:仅在当前用户编辑其他用户资料时触发,不适用于编辑自己的资料。
  • 参数:接受一个 $profile_user 参数,类型为 WP_User 对象,代表被编辑的用户。
  • 用途:常用于添加自定义表单字段或保存额外用户元数据到用户资料页面。
  • 相关钩子:如需应用于所有用户资料页面(包括当前用户),需同时使用 show_user_profile 钩子。
  • 版本历史:自 WordPress 2.0.0 版本引入。

代码示例

function custom_user_profile_fields( $profileuser ) {
?>
<h3>Custom Fields</h3>
<table class="form-table">
    <tr>
        <th><label for="twitter">Twitter</label></th>
        <td>
            <input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $profileuser->ID ) ); ?>" class="regular-text" />
        </td>
    </tr>
</table>
<?php
}
add_action( 'edit_user_profile', 'custom_user_profile_fields' );

注意事项

  • 避免在回调函数中错误使用 current_user_can 检查权限,应基于当前编辑用户的权限,而非被编辑用户的 ID。
  • 注意钩子适用范围:edit_user_profile 不适用于用户新建表单,相关操作应使用 user_new_form 钩子。

📄 原文内容

Fires after the ‘Application Passwords’ section is loaded on ‘Edit User’ screen.

Description

The action only fires if the current user is editing another user’s profile.

Parameters

$profile_userWP_User
The current WP_User object.

More Information

This action hook is typically used to output new fields or data to the bottom of WordPress’s user profile pages.

This hook only triggers when a user is viewing another users profile page (not their own). If you want to apply your hook to ALL profile pages (including the current user) then you also need to use the show_user_profile hook.

Source

do_action( 'edit_user_profile', $profile_user );

Changelog

Version Description
2.0.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    This action can be used to add a form to the backend user editing screen and save some additional user meta data.

    For example to add a field for the users birthday:

    function userMetaBirthdayForm(WP_User $user) {
    ?>
    

    Birthday

    <input type="date" value="ID, 'birthday', true)); ?>" name="user_birthday" id="user_birthday" > Some description to the input

  2. Skip to note 4 content

    Example migrated from Codex:

    /**
     * Show custom user profile fields
     * 
     * @param  object $profileuser A WP_User object
     * @return void
     */
    function custom_user_profile_fields( $profileuser ) {
    ?>
    	
    <input type="text" name="user_location" id="user_location" value="ID ) ); ?>" class="regular-text" />