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
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
- Scaling
- Constant
- Instructions
- 18–132
- Plugin surface
- 2 hooks
- Called by
- 0
Reaches the database via get_term().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 185.
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.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
isset($request) && !is_taxonomy_hierarchical() | 18 | is_taxonomy_hierarchical(), __() |
isset($request) && is_taxonomy_hierarchical() | 25 | is_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() | 42 | is_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() | 61 | is_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–65 | is_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) | 68 | is_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–80 | is_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) | 79 | is_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) | 94 | is_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–118 | is_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) | 132 | is_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 185 | 18–132 | 10 | |
| 8.5 | 185 | 18–132 | 10 | |
| 8.4 | 185 | 18–132 | 10 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 188 | 18–132 | 10 | |
| 8.2 | 188 | 18–132 | 10 | |
| 8.1 | 188 | 18–132 | 10 | |
| 7.4 | 188 | 18–132 | 10 |
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:
- 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.
- 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
- is_taxonomy_hierarchical()Determines whether the taxonomy object is hierarchical.
- __()Retrieves the translation of $text.
- wp_get_nav_menu_object()Returns a navigation menu object.
- wp_update_nav_menu_object()Saves the properties of a menu or create a new menu with those properties.
- 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.
- get_term_by()Gets all term data from database by term field and data.
- 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_Menus_Controller::prepare_item_for_database()Prepares a single term for create or update.
Show all 18
- WP_REST_Menus_Controller::get_term()Gets the term, if the ID is valid.
- WP_REST_Menus_Controller::get_item_schema()Retrieves the term's schema, conforming to JSON Schema.
- WP_REST_Menus_Controller::handle_locations()Updates the menu's locations from a REST request.
- WP_REST_Menus_Controller::handle_auto_add()Updates the menu's auto add from a REST request.
- WP_REST_Menus_Controller::update_additional_fields_for_object()
- WP_REST_Menus_Controller::prepare_item_for_response()Prepares a single term output for response.
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.
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.