返回介绍

wp_update_user()

发布于 2017-09-11 13:09:29 字数 6921 浏览 991 评论 0 收藏 0

wp_update_user( mixed $userdata )

Update a user in the database.


description

It is possible to update a user’s password by specifying the ‘user_pass’ value in the $userdata parameter array.

If current user’s password is being updated, then the cookies will be cleared.


参数

$userdata

(mixed) (Required) An array of user data or a user object of type stdClass or WP_User.


返回值

(int|WP_Error) The updated user's ID or a WP_Error object if the user could not be updated.


源代码

File: wp-includes/user.php

function wp_update_user($userdata) {
	if ( $userdata instanceof stdClass ) {
		$userdata = get_object_vars( $userdata );
	} elseif ( $userdata instanceof WP_User ) {
		$userdata = $userdata->to_array();
	}

	$ID = isset( $userdata['ID'] ) ? (int) $userdata['ID'] : 0;
	if ( ! $ID ) {
		return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) );
	}

	// First, get all of the original fields
	$user_obj = get_userdata( $ID );
	if ( ! $user_obj ) {
		return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) );
	}

	$user = $user_obj->to_array();

	// Add additional custom fields
	foreach ( _get_additional_user_keys( $user_obj ) as $key ) {
		$user[ $key ] = get_user_meta( $ID, $key, true );
	}

	// Escape data pulled from DB.
	$user = add_magic_quotes( $user );

	if ( ! empty( $userdata['user_pass'] ) && $userdata['user_pass'] !== $user_obj->user_pass ) {
		// If password is changing, hash it now
		$plaintext_pass = $userdata['user_pass'];
		$userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] );

		/**
		 * Filters whether to send the password change email.
		 *
		 * @since 4.3.0
		 *
		 * @see wp_insert_user() For `$user` and `$userdata` fields.
		 *
		 * @param bool  $send     Whether to send the email.
		 * @param array $user     The original user array.
		 * @param array $userdata The updated user array.
		 *
		 */
		$send_password_change_email = apply_filters( 'send_password_change_email', true, $user, $userdata );
	}

	if ( isset( $userdata['user_email'] ) && $user['user_email'] !== $userdata['user_email'] ) {
		/**
		 * Filters whether to send the email change email.
		 *
		 * @since 4.3.0
		 *
		 * @see wp_insert_user() For `$user` and `$userdata` fields.
		 *
		 * @param bool  $send     Whether to send the email.
		 * @param array $user     The original user array.
		 * @param array $userdata The updated user array.
		 *
		 */
		$send_email_change_email = apply_filters( 'send_email_change_email', true, $user, $userdata );
	}

	wp_cache_delete( $user['user_email'], 'useremail' );
	wp_cache_delete( $user['user_nicename'], 'userslugs' );

	// Merge old and new fields with new fields overwriting old ones.
	$userdata = array_merge( $user, $userdata );
	$user_id = wp_insert_user( $userdata );

	if ( ! is_wp_error( $user_id ) ) {

		$blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );

		$switched_locale = false;
		if ( ! empty( $send_password_change_email ) || ! empty( $send_email_change_email ) ) {
			$switched_locale = switch_to_locale( get_user_locale( $user_id ) );
		}

		if ( ! empty( $send_password_change_email ) ) {
			/* translators: Do not translate USERNAME, ADMIN_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */
			$pass_change_text = __( 'Hi

更新日志

Versiondescription
2.0.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: restore_previous_locale()
  • wp-includes/l10n.php: switch_to_locale()
  • wp-includes/l10n.php: get_user_locale()
  • wp-includes/user.php: send_email_change_email
  • wp-includes/user.php: password_change_email
  • wp-includes/user.php: email_change_email
  • wp-includes/user.php: send_password_change_email
  • wp-includes/cache.php: wp_cache_delete()
  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: wp_specialchars_decode()
  • wp-includes/pluggable.php: wp_hash_password()
  • wp-includes/pluggable.php: wp_clear_auth_cookie()
  • wp-includes/pluggable.php: wp_parse_auth_cookie()
  • wp-includes/pluggable.php: wp_set_auth_cookie()
  • wp-includes/pluggable.php: auth_cookie_expiration
  • wp-includes/pluggable.php: get_userdata()
  • wp-includes/pluggable.php: wp_mail()
  • wp-includes/pluggable.php: wp_get_current_user()
  • wp-includes/functions.php: add_magic_quotes()
  • wp-includes/link-template.php: home_url()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • wp-includes/user.php: _get_additional_user_keys()
  • wp-includes/user.php: wp_insert_user()
  • wp-includes/user.php: get_user_meta()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 22 more uses Hide more uses

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::create_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::update_item()
  • wp-admin/includes/user.php: edit_user()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_editProfile()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by franztt

    “If current user’s password is being updated, then the cookies will be cleared.” AND AN EMAIL WILL BE SEND !!!
    It sent an email to 50 users !!! Be more explicit !

  2. Example showing how to update a user’s website profile field:

    
    <?php
    $user_id = 6;
    $website = 'http://example.com';
    
    $user_data = wp_update_user( array( 'ID' => $user_id, 'user_url' => $website ) );
    
    if ( is_wp_error( $user_data ) ) {
    	// There was an error; possibly this user doesn't exist.
    	echo 'Error.';
    } else {
    	// Success!
    	echo 'User profile updated.';
    }
    ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文