Advertisements

wp_insert_term

Definition:
function wp_insert_term( $term, $taxonomy, $args = array() {}

Adds a new term to the database. Optionally marks it as an alias of an existing term.
Error handling is assigned for the nonexistence of the $taxonomy and $term parameters before inserting. If both the term id and taxonomy exist previously, then an array will be returned that contains the term id and the contents of what is returned. The keys of the array are ‘term_id’ and ‘term_taxonomy_id’ containing numeric values.

Parameters

  • string $term: The term to add or update.
  • string $taxonomy: The taxonomy to which to add the term
  • array|string $args: Change the values of the inserted term

Return values

returns:The Term ID and Term Taxonomy ID

Defined filters

  • pre_insert_term
    apply_filters( 'pre_insert_term', $term, $taxonomy )
  • term_id_filter
    apply_filters('term_id_filter', $term_id, $tt_id)

Defined actions

  • edit_terms
    do_action( 'edit_terms', $alias->term_id );
  • edited_terms
    do_action( 'edited_terms', $alias->term_id );
  • edit_terms
    do_action( 'edit_terms', $term_id );
  • edited_terms
    do_action( 'edited_terms', $term_id );
  • create_term
    do_action("create_term", $term_id, $tt_id, $taxonomy);
  • create_$taxonomy
    do_action("create_$taxonomy", $term_id, $tt_id);
  • created_term
    do_action("created_term", $term_id, $tt_id, $taxonomy);
  • created_$taxonomy
    do_action("created_$taxonomy", $term_id, $tt_id);

Source code

function wp_insert_term( $term, $taxonomy, $args = array() ) {

	global $wpdb;



	if ( ! taxonomy_exists($taxonomy) )

		return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));



	$term = apply_filters( 'pre_insert_term', $term, $taxonomy );

		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);

	$args['name'] = $term;

	$args['taxonomy'] = $taxonomy;

	$args = sanitize_term($args, $taxonomy, 'db');

	extract($args, EXTR_SKIP);



	// expected_slashed ($name)

	$name = stripslashes($name);

	$description = stripslashes($description);



	if ( empty($slug) )

		$slug = sanitize_title($name);



	$term_group = 0;

	if ( $alias_of ) {

		$alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );

		if ( $alias->term_group ) {

			// The alias we want is already in a group, so let's use that one.

			$term_group = $alias->term_group;

		} else {

			// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.

			$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;

			do_action( 'edit_terms', $alias->term_id );

			$wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );

			do_action( 'edited_terms', $alias->term_id );

		}

	}



	if ( $term_id = term_exists($slug) ) {

		$existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id), ARRAY_A );

		// We've got an existing term in the same taxonomy, which matches the name of the new term:

		if ( is_taxonomy_hierarchical($taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) {

			// Hierarchical, and it matches an existing term, Do not allow same "name" in the same level.

			$siblings = get_terms($taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => (int)$parent) );

			if ( in_array($name, $siblings) ) {

				return new WP_Error('term_exists', __('A term with the name provided already exists with this parent.'), $exists['term_id']);

			} else {

				$slug = wp_unique_term_slug($slug, (object) $args);

				if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )

					return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);

				$term_id = (int) $wpdb->insert_id;

			}

		} elseif ( $existing_term['name'] != $name ) {

			// We've got an existing term, with a different name, Create the new term.

			$slug = wp_unique_term_slug($slug, (object) $args);

			if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )

				return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);

			$term_id = (int) $wpdb->insert_id;

		} elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) )  {

			// Same name, same slug.

			return new WP_Error('term_exists', __('A term with the name provided already exists.'), $exists['term_id']);

		}

	} else {

		// This term does not exist at all in the database, Create it.

		$slug = wp_unique_term_slug($slug, (object) $args);

		if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )

			return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);

		$term_id = (int) $wpdb->insert_id;

	}



	// Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.

	if ( empty($slug) ) {

		$slug = sanitize_title($slug, $term_id);

		do_action( 'edit_terms', $term_id );

		$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );

		do_action( 'edited_terms', $term_id );

	}



	$tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) );



	if ( !empty($tt_id) )

		return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);



	$wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );

	$tt_id = (int) $wpdb->insert_id;



	do_action("create_term", $term_id, $tt_id, $taxonomy);

	do_action("create_$taxonomy", $term_id, $tt_id);



	$term_id = apply_filters('term_id_filter', $term_id, $tt_id);



	clean_term_cache($term_id, $taxonomy);



	do_action("created_term", $term_id, $tt_id, $taxonomy);

	do_action("created_$taxonomy", $term_id, $tt_id);



	return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);

}

3795

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: