WP_REST_Controller::update_additional_fields_for_object()protectedWP 4.7.0

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

Method of the class: WP_REST_Controller{}

No Hooks.

Return

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

Usage

// protected - for code of main (parent) or child class
$result = $this->update_additional_fields_for_object( $data_object, $request );
$data_object(object) (required)
Data model like WP_Term or WP_Post.
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 4.7.0 Introduced.

WP_REST_Controller::update_additional_fields_for_object() code WP 6.6.2

protected function update_additional_fields_for_object( $data_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 ],
			$data_object,
			$field_name,
			$request,
			$this->get_object_type()
		);

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

	return true;
}