wppaste
WordPress

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

Since
2.9.0
Source
wp-includes/meta.php:40
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.

Compatibility

WordPress
since 2.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$meta_typestring
Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.
$object_idint
ID of the object metadata is for.
$meta_keystring
Metadata key.
$meta_valuemixed
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.
$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.Default: false

Return value

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

Performance profile

How much work a call to add_metadata() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_var().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8–115

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 121.

Plugin surface
3 hooks

Third-party callbacks on 'add_{$meta_type}_metadata', 'add_{$meta_type}_meta', 'added_{$meta_type}_meta' run inside this call, and their cost is not bounded by anything here.

Called by
9

9 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • serializeserialisationmaybe_serialize()called directly
  • cacheobject cachewp_cache_delete()called directly
  • sqldatabase query->get_var()called directly
  • querycontent queryget_term()one call below add_metadata()

Further down the call graph this can also reach option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost add_metadata() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8–11none
$object_id16absint()
$object_id21absint(), _get_meta_table()
$object_id && $check !== null60absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters()
$object_id && $check === null && ->get_var()75absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), ->prepare(), ->get_var()
$object_id && $check === null84absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), maybe_serialize(), do_action()
$object_id && $check === null && !->get_var()98absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), ->prepare(), ->get_var(), maybe_serialize(), do_action()
$object_id && $check === null101absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), maybe_serialize(), do_action(), wp_cache_delete(), do_action()
$object_id && $check === null && !->get_var()115absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), ->prepare(), ->get_var(), maybe_serialize(), do_action(), wp_cache_delete(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1218–1159
8.51218–1159
8.41218–11592 fewer instructions than PHP 8.3
8.31238–1179
8.21238–1179
8.11238–1179
7.41238–1179

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while add_metadata() runs, in this order:

  1. apply_filters( add_{$meta_type}_metadata )filterline 90 (+50 into the body)

    Short-circuits adding metadata of a specific type.

  2. do_action( add_{$meta_type}_meta )actionline 128 (+88 into the body)

    Fires immediately before meta of a specific type is added.

  3. do_action( added_{$meta_type}_meta )actionline 168 (+128 into the body)

    Fires immediately after meta of a specific type is added.

Uses · 10

Used by · 9

Source code

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`

Changelog

Introduced in 2.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/meta.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.