返回介绍

update_user_meta()

发布于 2017-09-11 11:06:36 字数 4668 浏览 1411 评论 0 收藏 0

update_user_meta( int $user_id,  string $meta_key,  mixed $meta_value,  mixed $prev_value = '' )

Update user meta field based on user ID.


description

Use the $prev_value parameter to differentiate between meta fields with the same key and user ID.

If the meta field for the user does not exist, it will be added.


参数

$user_id

(int) (Required) User ID.

$meta_key

(string) (Required) Metadata key.

$meta_value

(mixed) (Required) Metadata value.

$prev_value

(mixed) (Optional) Previous value to check before removing.

Default value: ''


返回值

(int|bool) Meta ID if the key didn't exist, true on successful update, false on failure.


源代码

File: wp-includes/user.php

function update_user_meta($user_id, $meta_key, $meta_value, $prev_value = '') {
	return update_metadata('user', $user_id, $meta_key, $meta_value, $prev_value);
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/meta.php: update_metadata()

Used By

  • wp-admin/includes/class-wp-screen.php: WP_Screen::render_meta_boxes_preferences()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_wporg_username()
  • wp-includes/class-wp-user-meta-session-tokens.php: WP_User_Meta_Session_Tokens::update_sessions()
  • wp-admin/includes/ms.php: choose_primary_blog()
  • wp-admin/includes/ms.php: send_confirmation_on_profile_email()
  • wp-admin/includes/misc.php: set_screen_options()
  • wp-admin/includes/schema.php: populate_network()
  • wp-admin/includes/upgrade.php: wp_install_defaults()
  • wp-admin/includes/class-wp-plugin-install-list-table.php: WP_Plugin_Install_List_Table::prepare_items()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_user_color_scheme()
  • wp-admin/includes/ajax-actions.php: wp_ajax_dismiss_wp_pointer()
  • wp-admin/includes/ajax-actions.php: wp_ajax_update_welcome_panel()
  • wp-includes/class-wp-user.php: WP_User::update_user_level_from_caps()
  • wp-includes/class-wp-user.php: WP_User::add_cap()
  • wp-includes/class-wp-user.php: WP_User::remove_cap()
  • wp-includes/class-wp-user.php: WP_User::add_role()
  • wp-includes/class-wp-user.php: WP_User::remove_role()
  • wp-includes/class-wp-user.php: WP_User::set_role()
  • wp-includes/user.php: wp_insert_user()
  • wp-includes/user.php: update_user_option()
  • wp-includes/ms-functions.php: add_new_user_to_blog()
  • wp-includes/ms-functions.php: wpmu_create_blog()
  • wp-includes/ms-functions.php: get_active_blog_for_user()
  • wp-includes/ms-functions.php: add_user_to_blog()
  • wp-includes/ms-functions.php: remove_user_from_blog()
  • Show 20 more used by Hide more used by

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 Codex

    how to check for errors

    
    $user_id = 1;
    $new_value = 'some new value';
    
    // Will return false if the previous value is the same as $new_value.
    $updated = update_user_meta( $user_id, 'some_meta_key', $new_value );
    
    // So check and make sure the stored value matches $new_value.
    if ( $new_value != get_user_meta( $user_id,  'some_meta_key', true ) ) {
    	wp_die( __( 'An error occurred', 'textdomain' ) );
    }
    
  2. How to update a user’s Website profile field

    
    update_user_meta( $user_id = 1, 'user_url', $website = 'https://wordpress.org' );
    

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

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

发布评论

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