add_metadata() wordpress function and usage This text is not used because the data attribute has a value

add_metadata() wordpress function and usage

Home / WordPress / WordPress Wiki / add_metadata() wordpress function and usage

add_metadata( string $meta_type, int $object_id, string $meta_key, mixed $meta_value, bool $unique = false )
Adds metadata for the specified object.

Parameters

$meta_type

(string) (Required) Type of object metadata is for. Accepts ‘post’, ‘comment’, ‘term’, ‘user’, or any other object type with an associated meta table.

$object_id

(int) (Required) ID of the object metadata is for.

$meta_key

(string) (Required) Metadata key.

$meta_value

(mixed) (Required) Metadata value. Must be serializable if non-scalar.

$unique

(bool) (Optional) 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 value: false

Return

(int|false) The meta ID on success, false on failure.

Sample Usage

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 ) );
}

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments