wppaste
WordPress

wp_insert_term( string $term, string $taxonomy, array|string $args = array() ): array|WP_Error

Since
2.3.0
Source
wp-includes/taxonomy.php:2424
Adds a new term to the database.

Description

A non-existent term is inserted in the following sequence: <ol> <li>The term is added to the term table, then related to the taxonomy.</li> <li>If everything is correct, several actions are fired.</li> <li>The 'term_id_filter' is evaluated.</li> <li>The term cache is cleaned.</li> <li>Several more actions are fired.</li> <li>An array is returned containing the 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 slug

    Slug of the term to make this term an alias of.
  • $descriptionstringdefault: empty string

    The term description.
  • $parentintdefault: 0

    The id of the parent term.
  • $slugstringdefault: empty string

    The term slug to use.

Return

array|WP_Error
An array of the new term data, WP_Error otherwise.
  • $term_idint

    The new term ID.
  • $term_taxonomy_idint|string

    The new term taxonomy ID. Can be a numeric string.

Hooks fired · 12

12 hooks fire while wp_insert_term() runs, in this order:

  1. apply_filters( pre_insert_term )filterline 2441 (+17 into the body)

    Filters a term before it is sanitized and inserted into the database.

  2. apply_filters( wp_insert_term_data )filterline 2588 (+164 into the body)

    Filters term data before it is inserted into the database.

  3. do_action( edit_terms )actionline 2601 (+177 into the body)

    Fires immediately before the given terms are edited.

  4. do_action( edited_terms )actionline 2605 (+181 into the body)

    Fires immediately after a term is updated in the database, but before its term-taxonomy relationship is updated.

  5. apply_filters( wp_insert_term_duplicate_term_check )filterline 2647 (+223 into the body)

    Filters the duplicate term check that takes place during term creation.

  6. do_action( create_term )actionline 2677 (+253 into the body)

    Fires immediately after a new term is created, before the term cache is cleaned.

  7. do_action( create_{$taxonomy} )actionline 2697 (+273 into the body)

    Fires after a new term is created for a specific taxonomy.

  8. apply_filters( term_id_filter )filterline 2709 (+285 into the body)

    Filters the term ID after a new term is created.

  9. do_action( created_term )actionline 2727 (+303 into the body)

    Fires after a new term is created, and after the term cache has been cleaned.

  10. do_action( created_{$taxonomy} )actionline 2747 (+323 into the body)

    Fires after a new term in a specific taxonomy is created, and after the term cache has been cleaned.

  11. do_action( saved_term )actionline 2764 (+340 into the body)

    Fires after a term has been saved, and the term cache has been cleared.

  12. do_action( saved_{$taxonomy} )actionline 2785 (+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

Used by · 11

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.

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