Creates a single term in a taxonomy.
Parameters
$request
WP_REST_Requestrequired- Full details about the request.
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 = wp_get_nav_menu_object( (int) $request['parent'] );
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_update_nav_menu_object( 0, 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.
*/
if ( in_array( 'menu_exists', $term->get_error_codes(), true ) ) {
$existing_term = get_term_by( 'name', $prepared_term->{'menu-name'}, $this->taxonomy );
$term->add_data( $existing_term->term_id, 'menu_exists' );
$term->add_data(
array(
'status' => 400,
'term_id' => $existing_term->term_id,
)
);
} else {
$term->add_data( array( 'status' => 400 ) );
}
return $term;
}
$term = $this->get_term( $term );
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
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;
}
}
$locations_update = $this->handle_locations( $term->term_id, $request );
if ( is_wp_error( $locations_update ) ) {
return $locations_update;
}
$this->handle_auto_add( $term->term_id, $request );
$fields_update = $this->update_additional_fields_for_object( $term, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$request->set_param( 'context', 'view' );
/** 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, true );
$response = $this->prepare_item_for_response( $term, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) );
return $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.
Changelog
Version | Description |
---|---|
5.9.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.