wppaste
WordPress

_wp_ajax_add_hierarchical_term()

Since
3.1.0
Source
wp-admin/includes/ajax-actions.php:608
Handles adding a hierarchical term via AJAX.

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

Reaches the database via get_term().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
103–170

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

Plugin surface
1 hook

Third-party callbacks on 'post_edit_category_parent_dropdown_args' 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

  • querycontent queryget_term()called directly
  • hookthird-party callbacksapply_filters()called directly

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.

WhenInstructionsCalls it makes
current_user_can()103–121get_taxonomy(), check_ajax_referer(), current_user_can(), explode(), array_map(), wp_popular_terms_checklist(), apply_filters(), ob_start(), wp_dropdown_categories(), ob_get_clean(), newcat_parent(), ->send()
!current_user_can()106–124get_taxonomy(), check_ajax_referer(), current_user_can(), wp_die(), explode(), array_map(), wp_popular_terms_checklist(), apply_filters(), ob_start(), wp_dropdown_categories(), ob_get_clean(), newcat_parent(), ->send()
current_user_can() && !$parent136–154get_taxonomy(), check_ajax_referer(), current_user_can(), explode(), array_map(), wp_popular_terms_checklist(), get_term(), ob_start(), taxonomy(), ob_get_clean(), apply_filters(), ob_start(), wp_dropdown_categories(), ob_get_clean(), newcat_parent(), ->send()
!current_user_can() && !$parent139–157get_taxonomy(), check_ajax_referer(), current_user_can(), wp_die(), explode(), array_map(), wp_popular_terms_checklist(), get_term(), ob_start(), taxonomy(), ob_get_clean(), apply_filters(), ob_start(), wp_dropdown_categories(), ob_get_clean(), newcat_parent(), ->send()
current_user_can() && $parent && is_wp_error()149–167get_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(), apply_filters(), ob_start(), wp_dropdown_categories(), ob_get_clean(), newcat_parent(), ->send()
!current_user_can() && $parent && is_wp_error()152–170get_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(), apply_filters(), ob_start(), wp_dropdown_categories(), ob_get_clean(), newcat_parent(), ->send()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev234103–17016
8.5234103–17016
8.4234103–1701611 fewer instructions than PHP 8.3
8.3245106–17616
8.2245106–17616
8.1245106–176163 fewer instructions than PHP 7.4
7.4248106–17816

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 · 1

One hook fires while _wp_ajax_add_hierarchical_term() runs, in this order:

  1. apply_filters( post_edit_category_parent_dropdown_args )filterline 721 (+113 into the body)

    Filters the arguments for the taxonomy parent dropdown on the Post Edit page.

Uses · 13

Show all 13

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 $category_name ) {		$category_name     = trim( $category_name );		$category_nicename = sanitize_title( $category_name ); 		if ( '' === $category_nicename ) {			continue;		} 		$category_id = wp_insert_term( $category_name, $taxonomy->name, array( 'parent' => $parent ) ); 		if ( ! $category_id || is_wp_error( $category_id ) ) {			continue;		} else {			$category_id = $category_id['term_id'];		} 		$checked_categories[] = $category_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' => $category_id,				'selected_cats'        => $checked_categories,				'popular_cats'         => $popular_ids,			)		); 		$data = ob_get_clean(); 		$add = array(			'what'     => $taxonomy->name,			'id'       => $category_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.

  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 7.1.0 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.