wp_create_category

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

Description

(int|WP_Error) wp_create_category( (string) $cat_name, (int) $parent = 0 ); 

Returns (int|WP_Error)

Parameters (2)

0. $cat_name (string)
The cat name.
1. $parent — Optional. (int)
The parent.

Usage

  1. if ( !function_exists( 'wp_create_category' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/taxonomy.php'; 
  3.  
  4. // The cat name. 
  5. $cat_name = ''; 
  6.  
  7. // The parent. 
  8. $parent = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_create_category($cat_name, $parent); 
  12.  

Defined (1)

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

/wp-admin/includes/taxonomy.php  
  1. function wp_create_category( $cat_name, $parent = 0 ) { 
  2. if ( $id = category_exists($cat_name, $parent) ) 
  3. return $id; 
  4.  
  5. return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );