wppaste
WordPress

wp_insert_category( array $catarr, bool $wp_error = false ): int|WP_Error

Since
2.0.0, 2.5.0, 3.0.0
Source
wp-admin/includes/taxonomy.php:121
Updates an existing Category or creates a new Category.

Compatibility

WordPress
since 3.0.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

$catarrarray
Array of arguments for inserting a new category.
  • $cat_IDintdefault: 0

    Category ID. A non-zero value updates an existing category.
  • $taxonomystringdefault: 'category'

    Taxonomy slug.
  • $cat_namestringdefault: empty

    Category name.
  • $category_descriptionstringdefault: empty

    Category description.
  • $category_nicenamestringdefault: empty

    Category nice (display) name.
  • $category_parentint|stringdefault: empty

    Category parent ID.
$wp_errorbooloptional
Default false.Default: false

Return value

int|WP_Error
The ID number of the new or updated Category on success. Zero or a WP_Error on failure, depending on param $wp_error.

Performance profile

How much work a call to wp_insert_category() 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_terms().

Scaling
Constant

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

Instructions
14–82

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_insert_category()
  • querycontent queryget_terms()one call below wp_insert_category()

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 · 8 distinct outcomes

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

WhenInstructionsCalls it makes
always14wp_parse_args()
always21wp_parse_args(), __()
empty($parent)60–62wp_parse_args(), compact(), wp_insert_term(), is_wp_error()
empty($parent)61–63wp_parse_args(), compact(), wp_update_term(), is_wp_error()
!empty($parent)67–70wp_parse_args(), term_exists(), compact(), wp_insert_term(), is_wp_error()
!empty($parent)68–71wp_parse_args(), term_exists(), compact(), wp_update_term(), is_wp_error()
!empty($parent) && term_exists() && $catarr78–81wp_parse_args(), term_exists(), term_is_ancestor_of(), compact(), wp_insert_term(), is_wp_error()
!empty($parent) && term_exists() && $catarr79–82wp_parse_args(), term_exists(), term_is_ancestor_of(), compact(), wp_update_term(), is_wp_error()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10714–8210
8.510714–8210
8.410714–82102 fewer instructions than PHP 8.3
8.310916–8410
8.210916–8410
8.110916–8410
7.410916–8410

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

Used by · 3

Source code

function wp_insert_category( $catarr, $wp_error = false ) {	$cat_defaults = array(		'cat_ID'               => 0,		'taxonomy'             => 'category',		'cat_name'             => '',		'category_description' => '',		'category_nicename'    => '',		'category_parent'      => '',	);	$catarr       = wp_parse_args( $catarr, $cat_defaults ); 	if ( '' === trim( $catarr['cat_name'] ) ) {		if ( ! $wp_error ) {			return 0;		} else {			return new WP_Error( 'cat_name', __( 'You did not enter a category name.' ) );		}	} 	$catarr['cat_ID'] = (int) $catarr['cat_ID']; 	// Are we updating or creating?	$update = ! empty( $catarr['cat_ID'] ); 	$name        = $catarr['cat_name'];	$description = $catarr['category_description'];	$slug        = $catarr['category_nicename'];	$parent      = (int) $catarr['category_parent'];	if ( $parent < 0 ) {		$parent = 0;	} 	if ( empty( $parent )		|| ! term_exists( $parent, $catarr['taxonomy'] )		|| ( $catarr['cat_ID'] && term_is_ancestor_of( $catarr['cat_ID'], $parent, $catarr['taxonomy'] ) ) ) {		$parent = 0;	} 	$args = compact( 'name', 'slug', 'parent', 'description' ); 	if ( $update ) {		$catarr['cat_ID'] = wp_update_term( $catarr['cat_ID'], $catarr['taxonomy'], $args );	} else {		$catarr['cat_ID'] = wp_insert_term( $catarr['cat_name'], $catarr['taxonomy'], $args );	} 	if ( is_wp_error( $catarr['cat_ID'] ) ) {		if ( $wp_error ) {			return $catarr['cat_ID'];		} else {			return 0;		}	}	return $catarr['cat_ID']['term_id'];}

Changelog

Introduced in 2.0.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.

3.0.0
The 'taxonomy' argument was added.from the docblock
2.5.0
$wp_error parameter was added.from the docblock
2.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-admin/includes/taxonomy.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.