WordPress开发函数add_metadata()

WordPress开发函数add_metadata(),为指定的对象添加元数据。 用法: add_metada…

WordPress开发函数add_metadata(),为指定的对象添加元数据。

用法:

add_metadata( string $meta_type, int $object_id, string $meta_key, mixed $meta_value, bool $unique = false )

参数

$meta_type

(string) (必需) 对象元数据的类型为。接受’post’、’comment’、’term’、’user’或任何其他具有关联元表的对象类型。

$object_id

(int) (必需) 对象元数据的ID。

$meta_key

(string) (必需) 元数据的关键。

$meta_value

(mixed) (必需) 元数据的值。如果是非标量,则必须是可序列化的。

$unique

(bool) (可选) 指定的元数据键对于对象是否应该是唯一的。如果为true,并且对象已经有指定元数据键的值,则不会进行任何更改。

默认值: false

返回

(int|false) 成功时为元ID,失败时为false。

来源:

文件: wp-includes/meta.php

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, `$meta_type`, refers to the meta object type

* (post, comment, term, user, or any other type with an associated meta table).

* Returning a non-null value will effectively short-circuit the function.

*

* @since 3.1.0

*

* @param null|bool $check Whether to allow adding metadata for the given type.

* @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, `$meta_type`, refers to the meta object type

* (post, comment, term, user, or any other type with an associated meta table).

*

* @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. Serialized if non-scalar.

*/

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, `$meta_type`, refers to the meta object type

* (post, comment, term, user, or any other type with an associated meta table).

*

* @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. Serialized if non-scalar.

*/

do_action( “added_{$meta_type}_meta”, $mid, $object_id, $meta_key, $_meta_value );

return $mid;

}

更新日志:
WordPress开发函数add_metadata() (https://www.wpmee.com/) WordPress开发教程 第1张用户贡献的笔记

(由getmanzooronline贡献- 11个月前)

例子:

为注释添加额外的元值。这里的location是元键,$location是变量的值。

function add_comment_location($comment_id, $location) {

return add_metadata( ‘comment’, $comment_id, ‘location’, wp_slash( $location ) );

}

类别:WordPress函数讲解

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册