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
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: 0Category ID. A non-zero value updates an existing category.$taxonomystringdefault: 'category'Taxonomy slug.$cat_namestringdefault: emptyCategory name.$category_descriptionstringdefault: emptyCategory description.$category_nicenamestringdefault: emptyCategory nice (display) name.$category_parentint|stringdefault: emptyCategory 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
- Scaling
- Constant
- Instructions
- 14–82
- Plugin surface
- None
- Called by
- 3
Reaches the database via get_terms().
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 107.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_insert_category() - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14 | wp_parse_args() |
| always | 21 | wp_parse_args(), __() |
empty($parent) | 60–62 | wp_parse_args(), compact(), wp_insert_term(), is_wp_error() |
empty($parent) | 61–63 | wp_parse_args(), compact(), wp_update_term(), is_wp_error() |
!empty($parent) | 67–70 | wp_parse_args(), term_exists(), compact(), wp_insert_term(), is_wp_error() |
!empty($parent) | 68–71 | wp_parse_args(), term_exists(), compact(), wp_update_term(), is_wp_error() |
!empty($parent) && term_exists() && $catarr | 78–81 | wp_parse_args(), term_exists(), term_is_ancestor_of(), compact(), wp_insert_term(), is_wp_error() |
!empty($parent) && term_exists() && $catarr | 79–82 | wp_parse_args(), term_exists(), term_is_ancestor_of(), compact(), wp_update_term(), is_wp_error() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 107 | 14–82 | 10 | |
| 8.5 | 107 | 14–82 | 10 | |
| 8.4 | 107 | 14–82 | 10 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 109 | 16–84 | 10 | |
| 8.2 | 109 | 16–84 | 10 | |
| 8.1 | 109 | 16–84 | 10 | |
| 7.4 | 109 | 16–84 | 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.
Uses · 8
- wp_parse_args()Merges user defined arguments into defaults array.
- __()Retrieves the translation of $text.
- term_exists()Determines whether a taxonomy term exists.
- term_is_ancestor_of()Checks if a term is an ancestor of another term.
- wp_update_term()Updates term based on arguments provided.
- wp_insert_term()Adds a new term to the database.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- WP_Error::__construct()Initializes the error.
Used by · 3
- wp_create_category()Adds a new category to the database if it does not already exist.
- wp_update_category()Aliases wp_insert_category() with minimal args.
- wp_xmlrpc_server::wp_newCategory()Creates a new category.
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.
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/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.