WordPress at Your Fingertips
function is not described

WC_REST_Terms_Controller::create_item() public WC 1.0

Create a single term for a taxonomy.

{} It's a method of the class: WC_REST_Terms_Controller{}

Hooks from the method
Return

WP_REST_Request|WP_Error.

Usage

$WC_REST_Terms_Controller = new WC_REST_Terms_Controller();
$WC_REST_Terms_Controller->create_item( $request );
$request(WP_REST_Request) (required)
Full details about the request.

Code of WC_REST_Terms_Controller::create_item() WC 5.7.1

public function create_item( $request ) {
	$taxonomy = $this->get_taxonomy( $request );
	$name     = $request['name'];
	$args     = array();
	$schema   = $this->get_item_schema();

	if ( ! empty( $schema['properties']['description'] ) && isset( $request['description'] ) ) {
		$args['description'] = $request['description'];
	}
	if ( isset( $request['slug'] ) ) {
		$args['slug'] = $request['slug'];
	}
	if ( isset( $request['parent'] ) ) {
		if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
			return new WP_Error( 'woocommerce_rest_taxonomy_not_hierarchical', __( 'Can not set resource parent, taxonomy is not hierarchical.', 'woocommerce' ), array( 'status' => 400 ) );
		}
		$args['parent'] = $request['parent'];
	}

	$term = wp_insert_term( $name, $taxonomy, $args );
	if ( is_wp_error( $term ) ) {
		$error_data = array( 'status' => 400 );

		// If we're going to inform the client that the term exists,
		// give them the identifier they can actually use.
		$term_id = $term->get_error_data( 'term_exists' );
		if ( $term_id ) {
			$error_data['resource_id'] = $term_id;
		}

		return new WP_Error( $term->get_error_code(), $term->get_error_message(), $error_data );
	}

	$term = get_term( $term['term_id'], $taxonomy );

	$this->update_additional_fields_for_object( $term, $request );

	// Add term data.
	$meta_fields = $this->update_term_meta_fields( $term, $request );
	if ( is_wp_error( $meta_fields ) ) {
		wp_delete_term( $term->term_id, $taxonomy );

		return $meta_fields;
	}

	/**
	 * Fires after a single term is created or updated via the REST API.
	 *
	 * @param WP_Term         $term      Inserted Term object.
	 * @param WP_REST_Request $request   Request object.
	 * @param boolean         $creating  True when creating term, false when updating.
	 */
	do_action( "woocommerce_rest_insert_{$taxonomy}", $term, $request, true );

	$request->set_param( 'context', 'edit' );
	$response = $this->prepare_item_for_response( $term, $request );
	$response = rest_ensure_response( $response );
	$response->set_status( 201 );

	$base = '/' . $this->namespace . '/' . $this->rest_base;
	if ( ! empty( $request['attribute_id'] ) ) {
		$base = str_replace( '(?P<attribute_id>[\d]+)', (int) $request['attribute_id'], $base );
	}

	$response->header( 'Location', rest_url( $base . '/' . $term->term_id ) );

	return $response;
}