wp_insert_term( string $term, string $taxonomy, array|string $args = array() ): array|WP_Error
- Since
- 2.3.0
- Source
wp-includes/taxonomy.php:2458
Description
A non-existent term is inserted in the following sequence:
- The term is added to the term table, then related to the taxonomy.
- If everything is correct, several actions are fired.
- The 'term_id_filter' is evaluated.
- The term cache is cleaned.
- Several more actions are fired.
- An array is returned containing the
term_idandterm_taxonomy_id.
If the 'slug' argument is not empty, then it is checked to see if the term is invalid. If it is not a valid, existing term, it is added and the term_id is given.
If the taxonomy is hierarchical, and the 'parent' argument is not empty, the term is inserted and the term_id will be given.
Error handling: If $taxonomy does not exist or $term is empty, a WP_Error object will be returned.
If the term already exists on the same hierarchical level, or the term slug and name are not unique, a WP_Error object will be returned.
Compatibility
- WordPress
- since 2.3.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
$termstring- The term name to add.
$taxonomystring- The taxonomy to which to add the term.
$argsarray|stringoptional- Array or query string of arguments for inserting a term.Default:
array()$alias_ofstringdefault: empty string. Accepts a term slugSlug of the term to make this term an alias of.$descriptionstringdefault: empty stringThe term description.$parentintdefault: 0The id of the parent term.$slugstringdefault: empty stringThe term slug to use.
Return value
array|WP_Error- An array of the new term data, WP_Error otherwise.
$term_idintThe new term ID.$term_taxonomy_idint|stringThe new term taxonomy ID. Can be a numeric string.
Performance profile
How much work a call to wp_insert_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
- Constant
- Instructions
- 109–319
- Plugin surface
- 12 hooks
- Called by
- 11
Reaches the database via ->insert().
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 492.
Third-party callbacks on 'pre_insert_term', 'wp_insert_term_data', 'edit_terms' run inside this call, and their cost is not bounded by anything here.
11 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()called directly - querycontent query
get_terms()called directly - sqldatabase query
->insert()called directly - optionoption read or write
get_option()one call below wp_insert_term() - cacheobject cache
wp_cache_delete_multiple()one call below wp_insert_term()
Further down the call graph this can also reach 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 · 29 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_insert_term() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && !is_taxonomy_hierarchical() | 109–114 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), __(), get_terms() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args | 125–127 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), __(), wp_update_term() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && is_taxonomy_hierarchical() | 134–145 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), taxonomy(), wp_list_pluck(), wp_list_pluck(), __(), get_terms() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && $slug !== false | 136 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), __(), wp_update_term() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && !empty($slug) && !empty($tt_id) | 140–142 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && $slug !== false && !empty($slug) && !empty($tt_id) | 151 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && is_taxonomy_hierarchical() | 161–172 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), taxonomy(), wp_list_pluck(), wp_list_pluck(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), __(), wp_update_term() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && !empty($slug) && empty($tt_id) | 163–165 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var(), compact(), ->insert(), __(), term_id() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && empty($slug) && !empty($tt_id) | 170–172 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && $slug !== false && !empty($slug) && empty($tt_id) | 174 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var(), compact(), ->insert(), __(), term_id() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && is_taxonomy_hierarchical() && !empty($slug) && !empty($tt_id) | 176–187 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), taxonomy(), wp_list_pluck(), wp_list_pluck(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && $slug !== false && empty($slug) && !empty($tt_id) | 181 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var() |
17 further outcomes, up to 319 instructions
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && empty($slug) && empty($tt_id) | 193–195 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var(), compact(), ->insert(), __(), term_id() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && is_taxonomy_hierarchical() && !empty($slug) && empty($tt_id) | 199–210 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), taxonomy(), wp_list_pluck(), wp_list_pluck(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var(), compact(), ->insert(), __(), term_id() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && $slug !== false && empty($slug) && empty($tt_id) | 204 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var(), compact(), ->insert(), __(), term_id() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && is_taxonomy_hierarchical() && empty($slug) && !empty($tt_id) | 206–217 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), taxonomy(), wp_list_pluck(), wp_list_pluck(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && !empty($slug) && empty($tt_id) | 211–213 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), term_id(), term_taxonomy_id(), clean_term_cache() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && $slug !== false && !empty($slug) && empty($tt_id) | 222 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), term_id(), term_taxonomy_id(), clean_term_cache() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && is_taxonomy_hierarchical() && empty($slug) && empty($tt_id) | 229–240 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), taxonomy(), wp_list_pluck(), wp_list_pluck(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var(), compact(), ->insert(), __(), term_id() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && empty($slug) && empty($tt_id) | 241–243 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), term_id(), term_taxonomy_id(), clean_term_cache() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && !empty($slug) && empty($tt_id) | 242–244 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), do_action(), do_action(), apply_filters(), clean_term_cache(), do_action(), do_action(), do_action(), do_action() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && is_taxonomy_hierarchical() && !empty($slug) && empty($tt_id) | 247–258 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), taxonomy(), wp_list_pluck(), wp_list_pluck(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), term_id(), term_taxonomy_id(), clean_term_cache() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && $slug !== false && empty($slug) && empty($tt_id) | 252 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), term_id(), term_taxonomy_id(), clean_term_cache() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && $slug !== false && !empty($slug) && empty($tt_id) | 253 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), do_action(), do_action(), apply_filters(), clean_term_cache(), do_action(), do_action(), do_action(), do_action() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && empty($slug) && empty($tt_id) | 272–274 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), do_action(), do_action(), apply_filters(), clean_term_cache(), do_action(), do_action(), do_action(), do_action() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && is_taxonomy_hierarchical() && empty($slug) && empty($tt_id) | 277–288 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), taxonomy(), wp_list_pluck(), wp_list_pluck(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), term_id(), term_taxonomy_id(), clean_term_cache() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && is_taxonomy_hierarchical() && !empty($slug) && empty($tt_id) | 278–289 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), taxonomy(), wp_list_pluck(), wp_list_pluck(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), do_action(), do_action(), apply_filters(), clean_term_cache(), do_action(), do_action(), do_action(), do_action() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && $slug !== false && empty($slug) && empty($tt_id) | 283 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), do_action(), do_action(), apply_filters(), clean_term_cache(), do_action(), do_action(), do_action(), do_action() |
taxonomy_exists() && !is_wp_error() && !is_int($term) && $name !== "" && !$args && is_taxonomy_hierarchical() && empty($slug) && empty($tt_id) | 308–319 | taxonomy_exists(), apply_filters(), is_wp_error(), wp_parse_args(), sanitize_term(), wp_unslash(), wp_unslash(), taxonomy(), get_term_by(), is_taxonomy_hierarchical(), taxonomy(), wp_list_pluck(), wp_list_pluck(), wp_unique_term_slug(), compact(), apply_filters(), ->insert(), sanitize_title(), do_action(), compact(), compact(), ->update(), do_action(), ->prepare(), ->get_var(), compact(), ->insert(), ->prepare(), ->get_row(), apply_filters(), do_action(), do_action(), apply_filters(), clean_term_cache(), do_action(), do_action(), do_action(), do_action() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 492 | 109–319 | 32 | |
| 8.5 | 492 | 109–319 | 32 | |
| 8.4 | 492 | 109–319 | 32 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 500 | 111–327 | 32 | |
| 8.2 | 500 | 111–327 | 32 | |
| 8.1 | 500 | 111–327 | 32 | |
| 7.4 | 500 | 111–327 | 32 |
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 · 12
12 hooks fire while wp_insert_term() runs, in this order:
- apply_filters( pre_insert_term )filterline 2475 (+17 into the body)
Filters a term before it is sanitized and inserted into the database.
- apply_filters( wp_insert_term_data )filterline 2622 (+164 into the body)
Filters term data before it is inserted into the database.
- do_action( edit_terms )actionline 2635 (+177 into the body)
Fires immediately before the given terms are edited.
- do_action( edited_terms )actionline 2639 (+181 into the body)
Fires immediately after a term is updated in the database, but before its term-taxonomy relationship is updated.
- apply_filters( wp_insert_term_duplicate_term_check )filterline 2682 (+224 into the body)
Filters the duplicate term check that takes place during term creation.
- do_action( create_term )actionline 2712 (+254 into the body)
Fires immediately after a new term is created, before the term cache is cleaned.
- do_action( create_{$taxonomy} )actionline 2732 (+274 into the body)
Fires after a new term is created for a specific taxonomy.
- apply_filters( term_id_filter )filterline 2744 (+286 into the body)
Filters the term ID after a new term is created.
- do_action( created_term )actionline 2762 (+304 into the body)
Fires after a new term is created, and after the term cache has been cleaned.
- do_action( created_{$taxonomy} )actionline 2782 (+324 into the body)
Fires after a new term in a specific taxonomy is created, and after the term cache has been cleaned.
- do_action( saved_term )actionline 2799 (+341 into the body)
Fires after a term has been saved, and the term cache has been cleared.
- do_action( saved_{$taxonomy} )actionline 2820 (+362 into the body)
Fires after a term in a specific taxonomy has been saved, and the term cache has been cleared.
Uses · 18
- taxonomy_exists()Determines whether the taxonomy name exists.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_parse_args()Merges user defined arguments into defaults array.
- term_exists()Determines whether a taxonomy term exists.
- sanitize_term()Sanitizes all term fields.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
- get_term_by()Gets all term data from database by term field and data.
- wp_update_term()Updates term based on arguments provided.
- get_terms()Retrieves the terms in a given taxonomy or list of taxonomies.
Show all 18
- is_taxonomy_hierarchical()Determines whether the taxonomy object is hierarchical.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- wp_unique_term_slug()Makes term slug unique, if it isn't already.
- do_action()Calls the callback functions that have been added to an action hook.
- clean_term_cache()Removes all of the term IDs from the cache.
- WP_Error::__construct()Initializes the error.
Used by · 11
- WP_REST_Terms_Controller::create_item()Creates a single term in a taxonomy.
- _wp_ajax_add_hierarchical_term()Handles adding a hierarchical term via AJAX.
- register_taxonomy()Creates or modifies a taxonomy object.
- wp_ajax_add_link_category()Handles adding a link category via AJAX.
- wp_ajax_add_tag()Handles adding a tag via AJAX.
- wp_create_term()Adds a new term to the database if it does not already exist.
- wp_insert_category()Updates an existing Category or creates a new Category.
- wp_set_object_terms()Creates term and taxonomy relationships.
- wp_update_nav_menu_object()Saves the properties of a menu or create a new menu with those properties.
- wp_xmlrpc_server::_insert_post()Helper method for wp_newPost() and wp_editPost(), containing shared logic.
- wp_xmlrpc_server::wp_newTerm()Creates a new term.
Source code
function wp_insert_term( $term, $taxonomy, $args = array() ) { global $wpdb; if ( ! taxonomy_exists( $taxonomy ) ) { return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); } /** * Filters a term before it is sanitized and inserted into the database. * * @since 3.0.0 * @since 6.1.0 The `$args` parameter was added. * * @param string|WP_Error $term The term name to add, or a WP_Error object if there's an error. * @param string $taxonomy Taxonomy slug. * @param array|string $args Array or query string of arguments passed to wp_insert_term(). */ $term = apply_filters( 'pre_insert_term', $term, $taxonomy, $args ); if ( is_wp_error( $term ) ) { return $term; } if ( is_int( $term ) && 0 === $term ) { return new WP_Error( 'invalid_term_id', __( 'Invalid term ID.' ) ); } if ( '' === trim( $term ) ) { return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) ); } $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '', ); $args = wp_parse_args( $args, $defaults ); if ( (int) $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) { return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) ); } $args['name'] = $term; $args['taxonomy'] = $taxonomy; // Coerce null description to strings, to avoid database errors. $args['description'] = (string) $args['description']; $args = sanitize_term( $args, $taxonomy, 'db' ); // expected_slashed ($name) $name = wp_unslash( $args['name'] ); $description = wp_unslash( $args['description'] ); $parent = (int) $args['parent']; // Sanitization could clean the name to an empty string that must be checked again. if ( '' === $name ) { return new WP_Error( 'invalid_term_name', __( 'Invalid term name.' ) ); } $slug_provided = ! empty( $args['slug'] ); if ( ! $slug_provided ) { $slug = sanitize_title( $name ); } else { $slug = $args['slug']; } $term_group = 0; if ( $args['alias_of'] ) { $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy ); if ( ! empty( $alias->term_group ) ) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; } elseif ( ! empty( $alias->term_id ) ) { /* * The alias is not in a group, so we create a new one * and add the alias to it. */ $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;Changelog
Introduced in 2.3.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-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.