wp_insert_term( string $term, string $taxonomy, array|string $args = array() ): array|WP_Error
- Since
- 2.3.0
- Source
wp-includes/taxonomy.php:2407
Description
term_id and term_taxonomy_id.</li> </ol> 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.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
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.
Hooks fired · 12
12 hooks fire while wp_insert_term() runs, in this order:
- apply_filters( pre_insert_term )filterline 2424 (+17 into the body)
Filters a term before it is sanitized and inserted into the database.
- apply_filters( wp_insert_term_data )filterline 2571 (+164 into the body)
Filters term data before it is inserted into the database.
- do_action( edit_terms )actionline 2584 (+177 into the body)
Fires immediately before the given terms are edited.
- do_action( edited_terms )actionline 2588 (+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 2630 (+223 into the body)
Filters the duplicate term check that takes place during term creation.
- do_action( create_term )actionline 2660 (+253 into the body)
Fires immediately after a new term is created, before the term cache is cleaned.
- do_action( create_{$taxonomy} )actionline 2680 (+273 into the body)
Fires after a new term is created for a specific taxonomy.
- apply_filters( term_id_filter )filterline 2692 (+285 into the body)
Filters the term ID after a new term is created.
- do_action( created_term )actionline 2710 (+303 into the body)
Fires after a new term is created, and after the term cache has been cleaned.
- do_action( created_{$taxonomy} )actionline 2730 (+323 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 2747 (+340 into the body)
Fires after a term has been saved, and the term cache has been cleared.
- do_action( saved_{$taxonomy} )actionline 2768 (+361 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
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;History
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 6.7.7 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.