wp_create_term

Add a new term to the database if it does not already exist.

Description

(array|WP_Error) wp_create_term( (string) $tag_name, (string) $taxonomy = 'post_tag' ); 

Returns (array|WP_Error)

Parameters (2)

0. $tag_name (string)
The tag name.
1. $taxonomy — Optional. (string) => 'post_tag'
The taxonomy for which to retrieve terms. Default post_tag..

Usage

  1. if ( !function_exists( 'wp_create_term' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/taxonomy.php'; 
  3.  
  4. // The tag name. 
  5. $tag_name = ''; 
  6.  
  7. // Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. 
  8. $taxonomy = 'post_tag'; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_create_term($tag_name, $taxonomy); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/taxonomy.php  
  1. function wp_create_term($tag_name, $taxonomy = 'post_tag') { 
  2. if ( $id = term_exists($tag_name, $taxonomy) ) 
  3. return $id; 
  4.  
  5. return wp_insert_term($tag_name, $taxonomy);