钩子文档

wp_pre_insert_user_data

💡 云策文档标注

概述

wp_pre_insert_user_data 是一个 WordPress 过滤器钩子,用于在用户记录创建或更新前过滤用户数据。它仅处理 users 表中的数据,不包含用户元数据。

关键要点

  • 过滤器在 wp_insert_user() 函数中调用,允许开发者修改即将插入或更新到数据库的用户数据。
  • 参数包括 $data(用户数据数组)、$update(是否为更新操作)、$user_id(用户 ID,更新时提供)和 $userdata(原始用户数据数组)。
  • 支持的数据字段涵盖用户登录名、密码、邮箱、URL、显示名称、注册时间等核心属性,以及角色、本地化设置等扩展选项。
  • 从 WordPress 5.8.0 版本开始,新增了 $userdata 参数,提供更多上下文信息。

代码示例

$data = apply_filters( 'wp_pre_insert_user_data', $data, $update, ( $update ? $user_id : null ), $userdata );

注意事项

  • 此过滤器仅影响 users 表的数据,用户元数据需通过其他钩子处理。
  • 某些字段(如 rich_editing)接受字符串字面量 'true' 或 'false',而非布尔值。
  • 在 WordPress 6.8.0 中,用户密码默认使用 bcrypt 哈希算法替代 phpass。

📄 原文内容

Filters user data before the record is created or updated.

Description

It only includes data in the users table, not any user metadata.

Parameters

$dataarray
Values and keys for the user.

  • user_login string
    The user’s login. Only included if $update == false
  • user_pass string
    The user’s password.
  • user_email string
    The user’s email.
  • user_url string
    The user’s url.
  • user_nicename string
    The user’s nice name. Defaults to a URL-safe version of user’s login.
  • display_name string
    The user’s display name.
  • user_registered string
    MySQL timestamp describing the moment when the user registered. Defaults to the current UTC timestamp.

$updatebool
Whether the user is being updated rather than created.
$user_idint|null
ID of the user to be updated, or NULL if the user is being created.
$userdataarray
The raw array of data passed to wp_insert_user() .

More Arguments from wp_insert_user( … $userdata )

An array, object, or WP_User object of user data arguments.

  • ID int
    User ID. If supplied, the user will be updated.
  • user_pass string
    The plain-text user password for new users.
    Hashed password for existing users.
  • user_login string
    The user’s login username.
  • user_nicename string
    The URL-friendly user name.
  • user_url string
    The user URL.
  • user_email string
    The user email address.
  • display_name string
    The user’s display name.
    Default is the user’s username.
  • nickname string
    The user’s nickname.
    Default is the user’s username.
  • first_name string
    The user’s first name. For new users, will be used to build the first part of the user’s display name if $display_name is not specified.
  • last_name string
    The user’s last name. For new users, will be used to build the second part of the user’s display name if $display_name is not specified.
  • description string
    The user’s biographical description.
  • rich_editing string
    Whether to enable the rich-editor for the user.
    Accepts 'true' or 'false' as a string literal, not boolean. Default 'true'.
  • syntax_highlighting string
    Whether to enable the rich code editor for the user.
    Accepts 'true' or 'false' as a string literal, not boolean. Default 'true'.
  • comment_shortcuts string
    Whether to enable comment moderation keyboard shortcuts for the user. Accepts 'true' or 'false' as a string literal, not boolean. Default 'false'.
  • admin_color string
    Admin color scheme for the user. Default 'fresh'.
  • use_ssl bool
    Whether the user should always access the admin over https. Default false.
  • user_registered string
    Date the user registered in UTC. Format is ‘Y-m-d H:i:s’.
  • user_activation_key string
    Password reset key. Default empty.
  • spam bool
    Multisite only. Whether the user is marked as spam.
    Default false.
  • show_admin_bar_front string
    Whether to display the Admin Bar for the user on the site’s front end. Accepts 'true' or 'false' as a string literal, not boolean. Default 'true'.
  • role string
    User’s role.
  • locale string
    User’s locale. Default empty.
  • meta_input array
    Array of custom user meta values keyed by meta key.
    Default empty.

Source

$data = apply_filters( 'wp_pre_insert_user_data', $data, $update, ( $update ? $user_id : null ), $userdata );

Changelog

Version Description
6.8.0 The user’s password is now hashed using bcrypt by default instead of phpass.
5.8.0 The $userdata parameter was added.
4.9.0 Introduced.