WP_REST_Terms_Controller::create_item( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php:501
Creates a single term in a taxonomy.
Parameters
$requestWP_REST_Request- Full details about the request.
Return
WP_REST_Response|WP_Error- Response object on success, or WP_Error object on failure.
Hooks fired · 2
2 hooks fire while WP_REST_Terms_Controller::create_item() runs, in this order:
- do_action( rest_insert_{$this->taxonomy} )actionline 563 (+62 into the body)
Fires after a single term is created or updated via the REST API.
- do_action( rest_after_insert_{$this->taxonomy} )actionline 598 (+97 into the body)
Fires after a single term is completely created or updated via the REST API.
Uses · 14
- is_taxonomy_hierarchical()Determines whether the taxonomy object is hierarchical.
- __()Retrieves the translation of $text.
- get_term()Gets all term data from database by term ID.
- wp_insert_term()Adds a new term to the database.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- do_action()Calls the callback functions that have been added to an action hook.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- rest_url()Retrieves the URL to a REST endpoint.
- WP_Error::__construct()Initializes the error.
- WP_REST_Terms_Controller::prepare_item_for_database()Prepares a single term for create or update.
- WP_REST_Terms_Controller::get_item_schema()Retrieves the term's schema, conforming to JSON Schema.
Show all 14
- WP_REST_Terms_Controller::update_additional_fields_for_object()
- WP_REST_Terms_Controller::prepare_item_for_response()Prepares a single term output for response.
Source
public function create_item( $request ) { 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 ); $term = wp_insert_term( wp_slash( $prepared_term->name ), $this->taxonomy, wp_slash( (array) $prepared_term ) ); if ( is_wp_error( $term ) ) { /* * If we're going to inform the client that the term already exists, * give them the identifier for future use. */ $term_id = $term->get_error_data( 'term_exists' ); if ( $term_id ) { $existing_term = get_term( $term_id, $this->taxonomy ); $term->add_data( $existing_term->term_id, 'term_exists' ); $term->add_data( array( 'status' => 400, 'term_id' => $term_id, ) ); } return $term; } $term = get_term( $term['term_id'], $this->taxonomy ); /** * Fires after a single term is created or updated via the REST API. * * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. * * Possible hook names include: * * - `rest_insert_category` * - `rest_insert_post_tag` * * @since 4.7.0 * * @param WP_Term $term Inserted or updated term object. * @param WP_REST_Request $request Request object. * @param bool $creating True when creating a term, false when updating. */ do_action( "rest_insert_{$this->taxonomy}", $term, $request, true ); $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' );History
Introduced in 4.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.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.