wp/wp-admin/includes/taxonomy.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    15  *
    15  *
    16  * @since 2.0.0
    16  * @since 2.0.0
    17  *
    17  *
    18  * @see term_exists()
    18  * @see term_exists()
    19  *
    19  *
    20  * @param int|string $cat_name Category name.
    20  * @param int|string $cat_name        Category name.
    21  * @param int        $parent   Optional. ID of parent term.
    21  * @param int        $category_parent Optional. ID of parent category.
    22  * @return mixed
    22  * @return string|null Returns the category ID as a numeric string if the pairing exists, null if not.
    23  */
    23  */
    24 function category_exists( $cat_name, $parent = null ) {
    24 function category_exists( $cat_name, $category_parent = null ) {
    25 	$id = term_exists( $cat_name, 'category', $parent );
    25 	$id = term_exists( $cat_name, 'category', $category_parent );
    26 	if ( is_array( $id ) ) {
    26 	if ( is_array( $id ) ) {
    27 		$id = $id['term_id'];
    27 		$id = $id['term_id'];
    28 	}
    28 	}
    29 	return $id;
    29 	return $id;
    30 }
    30 }
    46 /**
    46 /**
    47  * Add a new category to the database if it does not already exist.
    47  * Add a new category to the database if it does not already exist.
    48  *
    48  *
    49  * @since 2.0.0
    49  * @since 2.0.0
    50  *
    50  *
    51  * @param int|string $cat_name
    51  * @param int|string $cat_name        Category name.
    52  * @param int        $parent
    52  * @param int        $category_parent Optional. ID of parent category.
    53  * @return int|WP_Error
    53  * @return int|WP_Error
    54  */
    54  */
    55 function wp_create_category( $cat_name, $parent = 0 ) {
    55 function wp_create_category( $cat_name, $category_parent = 0 ) {
    56 	$id = category_exists( $cat_name, $parent );
    56 	$id = category_exists( $cat_name, $category_parent );
    57 	if ( $id ) {
    57 	if ( $id ) {
    58 		return $id;
    58 		return $id;
    59 	}
    59 	}
    60 
    60 
    61 	return wp_insert_category(
    61 	return wp_insert_category(
    62 		array(
    62 		array(
    63 			'cat_name'        => $cat_name,
    63 			'cat_name'        => $cat_name,
    64 			'category_parent' => $parent,
    64 			'category_parent' => $category_parent,
    65 		)
    65 		)
    66 	);
    66 	);
    67 }
    67 }
    68 
    68 
    69 /**
    69 /**
   113  *     @type string     $category_description Category description. Default empty.
   113  *     @type string     $category_description Category description. Default empty.
   114  *     @type string     $category_nicename    Category nice (display) name. Default empty.
   114  *     @type string     $category_nicename    Category nice (display) name. Default empty.
   115  *     @type int|string $category_parent      Category parent ID. Default empty.
   115  *     @type int|string $category_parent      Category parent ID. Default empty.
   116  * }
   116  * }
   117  * @param bool  $wp_error Optional. Default false.
   117  * @param bool  $wp_error Optional. Default false.
   118  * @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure,
   118  * @return int|WP_Error The ID number of the new or updated Category on success. Zero or a WP_Error on failure,
   119  *                    depending on param $wp_error.
   119  *                      depending on param `$wp_error`.
   120  */
   120  */
   121 function wp_insert_category( $catarr, $wp_error = false ) {
   121 function wp_insert_category( $catarr, $wp_error = false ) {
   122 	$cat_defaults = array(
   122 	$cat_defaults = array(
   123 		'cat_ID'               => 0,
   123 		'cat_ID'               => 0,
   124 		'taxonomy'             => 'category',
   124 		'taxonomy'             => 'category',
   213  * Check whether a post tag with a given name exists.
   213  * Check whether a post tag with a given name exists.
   214  *
   214  *
   215  * @since 2.3.0
   215  * @since 2.3.0
   216  *
   216  *
   217  * @param int|string $tag_name
   217  * @param int|string $tag_name
   218  * @return mixed
   218  * @return mixed Returns null if the term does not exist.
       
   219  *               Returns an array of the term ID and the term taxonomy ID if the pairing exists.
       
   220  *               Returns 0 if term ID 0 is passed to the function.
   219  */
   221  */
   220 function tag_exists( $tag_name ) {
   222 function tag_exists( $tag_name ) {
   221 	return term_exists( $tag_name, 'post_tag' );
   223 	return term_exists( $tag_name, 'post_tag' );
   222 }
   224 }
   223 
   225