WP_REST_Controller::update_additional_fields_for_object() WordPress Method

The WP_REST_Controller::update_additional_fields_for_object() is a method used to update additional fields for a registered REST API object. This is useful for adding extra data to an object that is not part of the standard WordPress schema. For example, if you have a custom post type with additional meta fields, you can use this method to update those fields when a post is updated via the REST API.

WP_REST_Controller::update_additional_fields_for_object( object $object, WP_REST_Request $request ) #

Updates the values of additional fields added to a data object.


Parameters

$object

(object)(Required)Data model like WP_Term or WP_Post.

$request

(WP_REST_Request)(Required)Full details about the request.


Top ↑

Return

(true|WP_Error) True on success, WP_Error object if a field cannot be updated.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-controller.php

	protected function update_additional_fields_for_object( $object, $request ) {
		$additional_fields = $this->get_additional_fields();

		foreach ( $additional_fields as $field_name => $field_options ) {
			if ( ! $field_options['update_callback'] ) {
				continue;
			}

			// Don't run the update callbacks if the data wasn't passed in the request.
			if ( ! isset( $request[ $field_name ] ) ) {
				continue;
			}

			$result = call_user_func( $field_options['update_callback'], $request[ $field_name ], $object, $field_name, $request, $this->get_object_type() );

			if ( is_wp_error( $result ) ) {
				return $result;
			}
		}

		return true;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.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.