filter_default_metadata() WordPress Function

The filter_default_metadata() function allows you to filter the default values for a post's metadata fields. This can be useful if you want to change the default values for your post metadata fields, or if you want to add new fields to the default list.

filter_default_metadata( mixed $value, int $object_id, string $meta_key, bool $single, string $meta_type ) #

Filters into default_{$object_type}_metadata and adds in default value.


Parameters

$value

(mixed)(Required)Current value passed to filter.

$object_id

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

$meta_key

(string)(Required)Metadata key.

$single

(bool)(Required)If true, return only the first value of the specified $meta_key. This parameter has no effect if $meta_key is not specified.

$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.


Top ↑

Return

(mixed) An array of default values if $single is false. The default value of the meta field if $single is true.


Top ↑

Source

File: wp-includes/meta.php

function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) {
	global $wp_meta_keys;

	if ( wp_installing() ) {
		return $value;
	}

	if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) {
		return $value;
	}

	$defaults = array();
	foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) {
		foreach ( $meta_data as $_meta_key => $args ) {
			if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) {
				$defaults[ $sub_type ] = $args;
			}
		}
	}

	if ( ! $defaults ) {
		return $value;
	}

	// If this meta type does not have subtypes, then the default is keyed as an empty string.
	if ( isset( $defaults[''] ) ) {
		$metadata = $defaults[''];
	} else {
		$sub_type = get_object_subtype( $meta_type, $object_id );
		if ( ! isset( $defaults[ $sub_type ] ) ) {
			return $value;
		}
		$metadata = $defaults[ $sub_type ];
	}

	if ( $single ) {
		$value = $metadata['default'];
	} else {
		$value = array( $metadata['default'] );
	}

	return $value;
}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.