_wp_ajax_add_hierarchical_term()
- Since
- 3.1.0
- Source
wp-admin/includes/ajax-actions.php:590
Compatibility
- WordPress
- since 3.1.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.
Performance profile
How much work a call to _wp_ajax_add_hierarchical_term() 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
- Scales with input
- Instructions
- 97–164
- Plugin surface
- None
- Called by
- 0
Reaches the database via get_term().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 228.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_term()called directly - hookthird-party callbacks
do_action()one call below _wp_ajax_add_hierarchical_term()
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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_ajax_add_hierarchical_term() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
current_user_can() | 97–115 | get_taxonomy(), check_ajax_referer(), current_user_can(), explode(), array_map(), wp_popular_terms_checklist(), ob_start(), taxonomy(), ob_get_clean(), newcat_parent(), ->send() |
!current_user_can() | 100–118 | get_taxonomy(), check_ajax_referer(), current_user_can(), wp_die(), explode(), array_map(), wp_popular_terms_checklist(), ob_start(), taxonomy(), ob_get_clean(), newcat_parent(), ->send() |
current_user_can() && !$parent | 130–148 | get_taxonomy(), check_ajax_referer(), current_user_can(), explode(), array_map(), wp_popular_terms_checklist(), get_term(), ob_start(), taxonomy(), ob_get_clean(), ob_start(), taxonomy(), ob_get_clean(), newcat_parent(), ->send() |
!current_user_can() && !$parent | 133–151 | get_taxonomy(), check_ajax_referer(), current_user_can(), wp_die(), explode(), array_map(), wp_popular_terms_checklist(), get_term(), ob_start(), taxonomy(), ob_get_clean(), ob_start(), taxonomy(), ob_get_clean(), newcat_parent(), ->send() |
current_user_can() && $parent && is_wp_error() | 143–161 | get_taxonomy(), check_ajax_referer(), current_user_can(), explode(), array_map(), wp_popular_terms_checklist(), get_term(), get_term(), is_wp_error(), ob_start(), taxonomy(), ob_get_clean(), ob_start(), taxonomy(), ob_get_clean(), newcat_parent(), ->send() |
!current_user_can() && $parent && is_wp_error() | 146–164 | get_taxonomy(), check_ajax_referer(), current_user_can(), wp_die(), explode(), array_map(), wp_popular_terms_checklist(), get_term(), get_term(), is_wp_error(), ob_start(), taxonomy(), ob_get_clean(), ob_start(), taxonomy(), ob_get_clean(), newcat_parent(), ->send() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 228 | 97–164 | 16 | |
| 8.5 | 228 | 97–164 | 16 | |
| 8.4 | 228 | 97–164 | 16 | 11 fewer instructions than PHP 8.3 |
| 8.3 | 239 | 100–170 | 16 | |
| 8.2 | 239 | 100–170 | 16 | |
| 8.1 | 239 | 100–170 | 16 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 242 | 100–172 | 16 |
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.
Uses · 12
- get_taxonomy()Retrieves the taxonomy object of $taxonomy.
- check_ajax_referer()Verifies the Ajax request to prevent processing requests external of the blog.
- current_user_can()Returns whether the current user has the specified capability.
- wp_die()Kills WordPress execution and displays HTML page with an error message.
- wp_popular_terms_checklist()Retrieves a list of the most popular terms from the specified taxonomy.
- sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
- wp_insert_term()Adds a new term to the database.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_terms_checklist()Outputs an unordered list of checkbox input elements labelled with term names.
- get_term()Gets all term data from database by term ID.
- wp_dropdown_categories()Displays or retrieves the HTML dropdown list of categories.
- WP_Ajax_Response::__construct()Constructor - Passes args to WP_Ajax_Response::add().
Source code
function _wp_ajax_add_hierarchical_term() { $action = $_POST['action']; $taxonomy = get_taxonomy( substr( $action, 4 ) ); check_ajax_referer( $action, '_ajax_nonce-add-' . $taxonomy->name ); if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) { wp_die( -1 ); } $names = explode( ',', $_POST[ 'new' . $taxonomy->name ] ); $parent = isset( $_POST[ 'new' . $taxonomy->name . '_parent' ] ) ? (int) $_POST[ 'new' . $taxonomy->name . '_parent' ] : 0; if ( 0 > $parent ) { $parent = 0; } if ( 'category' === $taxonomy->name ) { $post_category = isset( $_POST['post_category'] ) ? (array) $_POST['post_category'] : array(); } else { $post_category = ( isset( $_POST['tax_input'] ) && isset( $_POST['tax_input'][ $taxonomy->name ] ) ) ? (array) $_POST['tax_input'][ $taxonomy->name ] : array(); } $checked_categories = array_map( 'absint', (array) $post_category ); $popular_ids = wp_popular_terms_checklist( $taxonomy->name, 0, 10, false ); foreach ( $names as $cat_name ) { $cat_name = trim( $cat_name ); $category_nicename = sanitize_title( $cat_name ); if ( '' === $category_nicename ) { continue; } $cat_id = wp_insert_term( $cat_name, $taxonomy->name, array( 'parent' => $parent ) ); if ( ! $cat_id || is_wp_error( $cat_id ) ) { continue; } else { $cat_id = $cat_id['term_id']; } $checked_categories[] = $cat_id; if ( $parent ) { // Do these all at once in a second. continue; } ob_start(); wp_terms_checklist( 0, array( 'taxonomy' => $taxonomy->name, 'descendants_and_self' => $cat_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids, ) ); $data = ob_get_clean(); $add = array( 'what' => $taxonomy->name, 'id' => $cat_id, 'data' => str_replace( array( "\n", "\t" ), '', $data ), 'position' => -1, ); } if ( $parent ) { // Foncy - replace the parent and all its children. $parent = get_term( $parent, $taxonomy->name ); $term_id = $parent->term_id; while ( $parent->parent ) { // Get the top parent. $parent = get_term( $parent->parent, $taxonomy->name ); if ( is_wp_error( $parent ) ) { break; } $term_id = $parent->term_id; }Changelog
Introduced in 3.1.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-admin/includes/ajax-actions.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.