add_metadata()
云策文档标注
概述
add_metadata() 函数用于为指定对象添加元数据,支持多种对象类型如 'post'、'user' 等。该函数处理元数据的序列化、唯一性检查,并触发相关 Hook 以扩展功能。
关键要点
- 参数包括 $meta_type(对象类型)、$object_id(对象ID)、$meta_key(元键)、$meta_value(元值)和可选的 $unique(是否唯一)。
- 元值支持标量和非标量类型,非标量数据会被序列化存储。
- 函数返回元ID(成功时)或 false(失败时)。
- 包含动态 Hook,如 add_{$meta_type}_metadata 用于短路检查,add_{$meta_type}_meta 和 added_{$meta_type}_meta 用于动作触发。
- 输入时元键和元值需转义斜杠,函数内部会使用 wp_unslash() 处理。
代码示例
function add_comment_location($comment_id, $location) {
return add_metadata( 'comment', $comment_id, 'location', wp_slash( $location ) );
}注意事项
- 确保 $meta_type 有效且关联元表存在,否则函数返回 false。
- 当 $unique 为 true 时,如果对象已存在相同元键,则不会添加新元数据。
- 元值存储时,false 转为空字符串,true 转为 '1',数字转为字符串。
原文内容
Adds metadata for the specified object.
Description
For historical reasons both the meta key and the meta value are expected to be “slashed” (slashes escaped) on input.
Parameters
$meta_typestringrequired-
Type of object metadata is for. Accepts
'blog','post','comment','term','user', or any other object type with an associated meta table. $object_idintrequired-
ID of the object metadata is for.
$meta_keystringrequired-
Metadata key.
$meta_valuemixedrequired-
Metadata value. Arrays and objects are stored as serialized data and will be returned as the same type when retrieved. Other data types will be stored as strings in the database:
- false is stored and retrieved as an empty string (
'') - true is stored and retrieved as
'1' - numbers (both integer and float) are stored and retrieved as strings Must be serializable if non-scalar.
- false is stored and retrieved as an empty string (
$uniquebooloptional-
Whether the specified metadata key should be unique for the object.
If true, and the object already has a value for the specified metadata key, no change will be made.Default:
false
Source
function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = false ) {
global $wpdb;
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
$meta_subtype = get_object_subtype( $meta_type, $object_id );
$column = sanitize_key( $meta_type . '_id' );
// expected_slashed ($meta_key)
$meta_key = wp_unslash( $meta_key );
$meta_value = wp_unslash( $meta_value );
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
/**
* Short-circuits adding metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (blog, post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible hook names include:
*
* - `add_blog_metadata`
* - `add_post_metadata`
* - `add_comment_metadata`
* - `add_term_metadata`
* - `add_user_metadata`
*
* @since 3.1.0
*
* @param null|int|false $check Whether to allow adding metadata for the given type. Return false or a meta ID
* to short-circuit the function. Return null to continue with the default behavior.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param bool $unique Whether the specified meta key should be unique for the object.
*/
$check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
if ( null !== $check ) {
return $check;
}
if ( $unique && $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
$meta_key,
$object_id
)
) ) {
return false;
}
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
/**
* Fires immediately before meta of a specific type is added.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (blog, post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `add_blog_meta`
* - `add_post_meta`
* - `add_comment_meta`
* - `add_term_meta`
* - `add_user_meta`
*
* @since 3.1.0
*
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
$result = $wpdb->insert(
$table,
array(
$column => $object_id,
'meta_key' => $meta_key,
'meta_value' => $meta_value,
)
);
if ( ! $result ) {
return false;
}
$mid = (int) $wpdb->insert_id;
wp_cache_delete( $object_id, $meta_type . '_meta' );
/**
* Fires immediately after meta of a specific type is added.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (blog, post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `added_blog_meta`
* - `added_post_meta`
* - `added_comment_meta`
* - `added_term_meta`
* - `added_user_meta`
*
* @since 2.9.0
*
* @param int $mid The meta ID after successful update.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
return $mid;
}
Hooks
- do_action( “added_{$meta_type}_meta”, int $mid, int $object_id, string $meta_key, mixed $_meta_value )
-
Fires immediately after meta of a specific type is added.
- do_action( “add_{$meta_type}_meta”, int $object_id, string $meta_key, mixed $_meta_value )
-
Fires immediately before meta of a specific type is added.
- apply_filters( “add_{$meta_type}_metadata”, null|int|false $check, int $object_id, string $meta_key, mixed $meta_value, bool $unique )
-
Short-circuits adding metadata of a specific type.
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |
Skip to note 2 content
getmanzooronline
Example:
Add an additional meta value for comments. Here location is the meta key $location is the variable holds its value.
function add_comment_location($comment_id, $location) { return add_metadata( 'comment', $comment_id, 'location', wp_slash( $location ) ); }