wp_create_category() WordPress Function

The wp_create_category() function allows you to create a new category in WordPress. This function takes two arguments: the name of the category and the parent category. The name argument is required, while the parent category is optional. If you don't specify a parent category, the new category will be created as a top-level category.

wp_create_category( int|string $cat_name, int $category_parent ) #

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


Parameters

$cat_name

(int|string)(Required)Category name.

$category_parent

(int)(Optional) ID of parent category.


Top ↑

Return

(int|WP_Error)


Top ↑

More Information

Parameters:

  • $cat_name: Name for the new category.
  • $parent: Category ID of the parent category.

Returns:

  • 0 on failure, category id on success.

wp_create_category() is a thin wrapper around wp_insert_category().

Because this is a wrapper, it is not suitable for entering a complex custom taxonomy element.

If the category already exists, it is not duplicated. The ID of the original existing category is returned without error.

 


Top ↑

Source

File: wp-admin/includes/taxonomy.php

function wp_create_category( $cat_name, $category_parent = 0 ) {
	$id = category_exists( $cat_name, $category_parent );
	if ( $id ) {
		return $id;
	}

	return wp_insert_category(
		array(
			'cat_name'        => $cat_name,
			'category_parent' => $category_parent,
		)
	);
}


Top ↑

Changelog

Changelog
VersionDescription
2.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.