WordPress Version: 6.1
/**
* Adds a new term to the database if it does not already exist.
*
* @since 2.8.0
*
* @param string $tag_name The term name.
* @param string $taxonomy Optional. The taxonomy within which to create the term. Default 'post_tag'.
* @return array|WP_Error
*/
function wp_create_term($tag_name, $taxonomy = 'post_tag')
{
$id = term_exists($tag_name, $taxonomy);
if ($id) {
return $id;
}
return wp_insert_term($tag_name, $taxonomy);
}