wppaste
WordPress

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:527
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:

  1. do_action( rest_insert_{$this->taxonomy} )actionline 589 (+62 into the body)

    Fires after a single term is created or updated via the REST API.

  2. do_action( rest_after_insert_{$this->taxonomy} )actionline 624 (+97 into the body)

    Fires after a single term is completely created or updated via the REST API.

Uses · 14

Show all 14

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.