WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp insert category

Description

Inserts a new category into the taxonomy system.

Usage

<?php wp_insert_category$catarr$wp_error ); ?>

Parameters

$catarr
(array) (required) Category information.
Default: None
$catarr is checked against an array with the following default values:
$cat_defaults = array(
  'cat_ID' => 0,
  'cat_name' => ,
  'category_description' => ,
  'category_nicename' => ,
  'category_parent' => ,
  'taxonomy' => 'category' );
$catarr may contain additional values, but it is recommended that you provide at least the minimum of those defined in $cat_defaults. Note that category_parent is a category ID.
The cat_ID drives only the update and not the creation. If it's set, the function considers the call as an update of an existing category, the category cat_ID. Otherwise it's a category creation.
$wp_error
(boolean) (optional) Whether to return a WP_Error or a boolean. Boolean by default.
Default: false

Return Values

(boolean | WP_Error | int) 
On success returns the Category ID assigned to the new category or the original Category ID if it was an update. On failure returns false or a WP_Error if the second parameter (wp_error) is true.

Examples

To insert a new category use:

%%%//Define the category
$my_cat = array('cat_name' => 'My Category', 'category_description' => 'A Cool Category', 'category_nicename' => 'category-slug', 'category_parent' => '');

// Create the category
$my_cat_id = wp_insert_category($my_cat);

Notes

  • Not all possible members of the $catarr array are listed here.
  • See wp_create_category() for a simpler version which takes just a string instead of an array.

Source File

wp_insert_category() is located in wp-admin/includes/taxonomy.php.

Related

See also index of Function Reference and index of Template Tags.