wppaste
WordPress

add_meta( int $post_id ): int|bool

Since
1.2.0
Source
wp-admin/includes/post.php:1008
Adds post meta data defined in the $_POST superglobal for a post with given ID.

Parameters

$post_idint

Return

int|bool

Uses · 5

  • wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
  • is_protected_meta()Determines whether a meta key is considered protected.
  • current_user_can()Returns whether the current user has the specified capability.
  • wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
  • add_post_meta()Adds a meta field to the given post.

Used by · 3

Source

function add_meta( $post_id ) {	$post_id = (int) $post_id; 	$metakeyselect = isset( $_POST['metakeyselect'] ) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';	$metakeyinput  = isset( $_POST['metakeyinput'] ) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';	$metavalue     = $_POST['metavalue'] ?? '';	if ( is_string( $metavalue ) ) {		$metavalue = trim( $metavalue );	} 	if ( ( ( '#NONE#' !== $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) {		/*		 * We have a key/value pair. If both the select and the input		 * for the key have data, the input takes precedence.		 */		if ( '#NONE#' !== $metakeyselect ) {			$metakey = $metakeyselect;		} 		if ( $metakeyinput ) {			$metakey = $metakeyinput; // Default.		} 		if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_id, $metakey ) ) {			return false;		} 		$metakey = wp_slash( $metakey ); 		return add_post_meta( $post_id, $metakey, $metavalue );	} 	return false;}

History

Introduced in 1.2.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.1.0 tag, from src/wp-admin/includes/post.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.