wppaste
WordPress

WP_REST_Menus_Controller::create_item( WP_REST_Request $request ): WP_REST_Response|WP_Error

Since
5.9.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php:204
Creates a single term in a taxonomy.

Compatibility

WordPress
since 5.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$requestWP_REST_Request
Full details about the request.

Return value

WP_REST_Response|WP_Error
Response object on success, or WP_Error object on failure.

Performance profile

How much work a call to WP_REST_Menus_Controller::create_item() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_term().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
18–132

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 185.

Plugin surface
2 hooks

Third-party callbacks on 'rest_insert_{$this->taxonomy}', 'rest_after_insert_{$this->taxonomy}' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • querycontent queryget_term()one call below WP_REST_Menus_Controller::create_item()

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 20 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Menus_Controller::create_item() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
isset($request) && !is_taxonomy_hierarchical()18is_taxonomy_hierarchical(), __()
isset($request) && is_taxonomy_hierarchical()25is_taxonomy_hierarchical(), wp_get_nav_menu_object(), __()
!isset($request) && is_wp_error()29->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_error_codes(), ->add_data()
isset($request) && is_taxonomy_hierarchical() && is_wp_error()42is_taxonomy_hierarchical(), wp_get_nav_menu_object(), ->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_error_codes(), ->add_data()
!isset($request) && is_wp_error()48->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_error_codes(), get_term_by(), ->add_data(), status()
!isset($request)50–52->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->handle_locations(), is_wp_error()
!empty($value)55->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error()
isset($request) && is_taxonomy_hierarchical() && is_wp_error()61is_taxonomy_hierarchical(), wp_get_nav_menu_object(), ->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_error_codes(), get_term_by(), ->add_data(), status()
isset($request) && is_taxonomy_hierarchical()63–65is_taxonomy_hierarchical(), wp_get_nav_menu_object(), ->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->handle_locations(), is_wp_error()
!isset($request)65–67->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->handle_locations(), is_wp_error(), ->handle_auto_add(), ->update_additional_fields_for_object(), is_wp_error()
!empty($value)66->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->handle_locations(), is_wp_error()
isset($request) && is_taxonomy_hierarchical() && !empty($value)68is_taxonomy_hierarchical(), wp_get_nav_menu_object(), ->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error()
8 further outcomes, up to 132 instructions
isset($request) && is_taxonomy_hierarchical()78–80is_taxonomy_hierarchical(), wp_get_nav_menu_object(), ->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->handle_locations(), is_wp_error(), ->handle_auto_add(), ->update_additional_fields_for_object(), is_wp_error()
isset($request) && is_taxonomy_hierarchical() && !empty($value)79is_taxonomy_hierarchical(), wp_get_nav_menu_object(), ->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->handle_locations(), is_wp_error()
!empty($value)81->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->handle_locations(), is_wp_error(), ->handle_auto_add(), ->update_additional_fields_for_object(), is_wp_error()
isset($request) && is_taxonomy_hierarchical() && !empty($value)94is_taxonomy_hierarchical(), wp_get_nav_menu_object(), ->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->handle_locations(), is_wp_error(), ->handle_auto_add(), ->update_additional_fields_for_object(), is_wp_error()
!isset($request) && !is_wp_error()103–105->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->handle_locations(), is_wp_error(), ->handle_auto_add(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()
isset($request) && is_taxonomy_hierarchical() && !is_wp_error()116–118is_taxonomy_hierarchical(), wp_get_nav_menu_object(), ->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->handle_locations(), is_wp_error(), ->handle_auto_add(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()
!is_wp_error() && !empty($value)119->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->handle_locations(), is_wp_error(), ->handle_auto_add(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()
isset($request) && is_taxonomy_hierarchical() && !is_wp_error() && !empty($value)132is_taxonomy_hierarchical(), wp_get_nav_menu_object(), ->prepare_item_for_database(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_term(), do_action(), ->get_item_schema(), ->update_value(), is_wp_error(), ->handle_locations(), is_wp_error(), ->handle_auto_add(), ->update_additional_fields_for_object(), is_wp_error(), ->set_param(), do_action(), ->prepare_item_for_response(), rest_ensure_response(), ->set_status(), rest_url(), ->header()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev18518–13210
8.518518–13210
8.418518–132103 fewer instructions than PHP 8.3
8.318818–13210
8.218818–13210
8.118818–13210
7.418818–13210

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while WP_REST_Menus_Controller::create_item() runs, in this order:

  1. do_action( rest_insert_{$this->taxonomy} )actionline 246 (+42 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 274 (+70 into the body)

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

Uses · 18

Show all 18

Source code

	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;	}

Changelog

Introduced in 5.9.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 6.8.8 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-menus-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.