On this page
WP_REST_Terms_Controller::update_item( WP_REST_Request $request ): WP_REST_Response|WP_Error
Updates a single term from a taxonomy.
Parameters
$requestWP_REST_Request Required-
Full details about the request.
Return
WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php. View all references
public function update_item( $request ) {
$term = $this->get_term( $request['id'] );
if ( is_wp_error( $term ) ) {
return $term;
}
if ( isset( $request['parent'] ) ) {
if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
return new WP_Error(
'rest_taxonomy_not_hierarchical',
__( 'Cannot set parent term, taxonomy is not hierarchical.' ),
array( 'status' => 400 )
);
}
$parent = get_term( (int) $request['parent'], $this->taxonomy );
if ( ! $parent ) {
return new WP_Error(
'rest_term_invalid',
__( 'Parent term does not exist.' ),
array( 'status' => 400 )
);
}
}
$prepared_term = $this->prepare_item_for_database( $request );
// Only update the term if we have something to update.
if ( ! empty( $prepared_term ) ) {
$update = wp_update_term( $term->term_id, $term->taxonomy, wp_slash( (array) $prepared_term ) );
if ( is_wp_error( $update ) ) {
return $update;
}
}
$term = get_term( $term->term_id, $this->taxonomy );
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
do_action( "rest_insert_{$this->taxonomy}", $term, $request, false );
$schema = $this->get_item_schema();
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
$meta_update = $this->meta->update_value( $request['meta'], $term->term_id );
if ( is_wp_error( $meta_update ) ) {
return $meta_update;
}
}
$fields_update = $this->update_additional_fields_for_object( $term, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$request->set_param( 'context', 'edit' );
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, false );
$response = $this->prepare_item_for_response( $term, $request );
return rest_ensure_response( $response );
}
Hooks
- do_action( "rest_after_insert_{$this->taxonomy}",
WP_Term $term ,WP_REST_Request $request ,bool $creating ) -
Fires after a single term is completely created or updated via the REST API.
- do_action( "rest_insert_{$this->taxonomy}",
WP_Term $term ,WP_REST_Request $request ,bool $creating ) -
Fires after a single term is created or updated via the REST API.
Related
Uses
| Uses | Description |
|---|---|
| WP_REST_Terms_Controller::get_term() wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php | Get the term, if the ID is valid. |
| WP_REST_Terms_Controller::get_item_schema() wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php | Retrieves the term’s schema, conforming to JSON Schema. |
| WP_REST_Terms_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php | Prepares a single term output for response. |
| WP_REST_Terms_Controller::prepare_item_for_database() wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php | Prepares a single term for create or update. |
| wp_update_term() wp-includes/taxonomy.php | Updates term based on arguments provided. |
| is_taxonomy_hierarchical() wp-includes/taxonomy.php | Determines whether the taxonomy object is hierarchical. |
| rest_ensure_response() wp-includes/rest-api.php | Ensures a REST response is a response object (for consistency). |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| wp_slash() wp-includes/formatting.php | Adds slashes to a string or recursively adds slashes to strings within an array. |
| get_term() wp-includes/taxonomy.php | Gets all term data from database by term ID. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_terms_controller/update_item