問題描述
任何人都可以解釋 update_user_meta 和 update_user_option 有什麼區別,哪些情況可以使用?
最佳解決方案
在外行方面沒有什麼重大的區別! update_user_option()內部使用 update_user_meta()。唯一的區別是 update_user_option()字首選項名稱與資料庫表字首+部落格 ID,如果您是在多站點和只是表字首,如果您在單站點安裝。
看看 update_user_option()的程式碼
/**
* Update user option with global blog capability.
*
* User options are just like user metadata except that they have support for
* global blog options. If the 'global' parameter is false, which it is by default
* it will prepend the WordPress table prefix to the option name.
*
* Deletes the user option if $newvalue is empty.
*
* @since 2.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $user_id User ID.
* @param string $option_name User option name.
* @param mixed $newvalue User option value.
* @param bool $global Optional. Whether option name is global or blog specific.
* Default false (blog specific).
* @return int|bool User meta ID if the option didn't exist, true on successful update,
* false on failure.
*/
function update_user_option( $user_id, $option_name, $newvalue, $global = false ) {
global $wpdb;
if ( !$global )
$option_name = $wpdb->get_blog_prefix() . $option_name;
return update_user_meta( $user_id, $option_name, $newvalue );
}
您的選項名稱字首為表字首+部落格 ID(僅當 ID 不是 1 和 0 時) 。
如果將最後一個引數 $global 設定為 true,與 update_user_meta()沒有任何差別。
update_user_option()功能的目的
與其他表不同,WordPress 不為每個站點的 usermeta 建立單獨的表。它將使用者資訊儲存在所有部落格的一個 usermeta 表中 (在多個站點中) 。它只是用 blog prefix 例如每個站點的鍵名稱字首。部落格 ID 4 wp_capabilities 儲存為 wp_4_capabilities 。
所以無論您使用 update_user_option()儲存的資訊,例如 key_name_abc 將成為多站點或單站安裝中的主站點的 wp_key_name_abc 。將來如果您將單個站點轉換為多站點,資訊將僅在主站點上可用。
當您認為某些資訊依賴於 site +使用者時,請使用此功能。不像名字,電子郵件等,因為這些資訊屬於使用者和站點獨立。
次佳解決方案
兩者都將資料寫入 「usermeta」 表中。儲存在 usermeta 表中的使用者選項保留 wordpress 表字首,例如而使用者 meta 也儲存在 usermeta 表中。
使用者選項支援 blog-specific 選項,在多站點中有用。使用者元基於使用者 ID 特定的後設資料,如簡檔資訊。
事實上這些引數是非常不同的。使用者選項有 $ user_id,$ option_name,$ newvalue,$ global 和 user meta 有 $ user_id,$ meta_key,$ meta_value,$ prev_value 。
以下是這兩個選項和使用者 usermeta 的一些值。
選項
-
wp_user_level
-
wp_user-settings
-
wp_capabilities
-
wp_user-settings-time
使用者
-
first_name
-
last_name
-
暱稱
-
rich_editing
-
show_admin_bar_front
-
admin_color
https://codex.wordpress.org/Function_Reference/update_user_option#Parameters
https://codex.wordpress.org/Function_Reference/update_user_meta#Parameters
抄本頁的例子提供了現實世界的使用。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。