3 * WordPress Taxonomy Administration API.
6 * @subpackage Administration
14 * {@internal Missing Short Description}}
18 * @param int|string $cat_name
21 function category_exists($cat_name, $parent = 0) {
22 $id = term_exists($cat_name, 'category', $parent);
29 * {@internal Missing Short Description}}
36 function get_category_to_edit( $id ) {
37 $category = get_term( $id, 'category', OBJECT, 'edit' );
38 _make_cat_compat( $category );
43 * {@internal Missing Short Description}}
47 * @param int|string $cat_name
49 * @return int|WP_Error
51 function wp_create_category( $cat_name, $parent = 0 ) {
52 if ( $id = category_exists($cat_name, $parent) )
55 return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
59 * Create categories for the given post.
63 * @param array $categories List of categories to create.
64 * @param int $post_id Optional. The post ID. Default empty.
65 * @return List of categories to create for the given post.
67 function wp_create_categories( $categories, $post_id = '' ) {
69 foreach ($categories as $category) {
70 if ($id = category_exists($category))
73 if ($id = wp_create_category($category))
78 wp_set_post_categories($post_id, $cat_ids);
84 * Updates an existing Category or creates a new Category.
87 * @since 2.5.0 $wp_error parameter was added.
88 * @since 3.0.0 The 'taxonomy' argument was added.
90 * @param array $catarr {
91 * Array of arguments for inserting a new category.
93 * @type int $cat_ID Categoriy ID. A non-zero value updates an existing category.
95 * @type string $taxonomy Taxonomy slug. Defualt 'category'.
96 * @type string $cat_nam Category name. Default empty.
97 * @type string $category_description Category description. Default empty.
98 * @type string $category_nicename Category nice (display) name. Default empty.
99 * @type int|string $category_parent Category parent ID. Default empty.
101 * @param bool $wp_error Optional. Default false.
102 * @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure,
103 * depending on param $wp_error.
105 function wp_insert_category( $catarr, $wp_error = false ) {
106 $cat_defaults = array( 'cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '' );
107 $catarr = wp_parse_args( $catarr, $cat_defaults );
109 if ( trim( $catarr['cat_name'] ) == '' ) {
113 return new WP_Error( 'cat_name', __( 'You did not enter a category name.' ) );
117 $catarr['cat_ID'] = (int) $catarr['cat_ID'];
119 // Are we updating or creating?
120 $update = ! empty ( $catarr['cat_ID'] );
122 $name = $catarr['cat_name'];
123 $description = $catarr['category_description'];
124 $slug = $catarr['category_nicename'];
125 $parent = (int) $catarr['category_parent'];
130 if ( empty( $parent )
131 || ! term_exists( $parent, $catarr['taxonomy'] )
132 || ( $catarr['cat_ID'] && term_is_ancestor_of( $catarr['cat_ID'], $parent, $catarr['taxonomy'] ) ) ) {
136 $args = compact('name', 'slug', 'parent', 'description');
139 $catarr['cat_ID'] = wp_update_term( $catarr['cat_ID'], $catarr['taxonomy'], $args );
141 $catarr['cat_ID'] = wp_insert_term( $catarr['cat_name'], $catarr['taxonomy'], $args );
144 if ( is_wp_error( $catarr['cat_ID'] ) ) {
146 return $catarr['cat_ID'];
151 return $catarr['cat_ID']['term_id'];
155 * Aliases wp_insert_category() with minimal args.
157 * If you want to update only some fields of an existing category, call this
158 * function with only the new values set inside $catarr.
162 * @param array $catarr The 'cat_ID' value is required. All other keys are optional.
163 * @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
165 function wp_update_category($catarr) {
166 $cat_ID = (int) $catarr['cat_ID'];
168 if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
171 // First, get all of the original fields
172 $category = get_term( $cat_ID, 'category', ARRAY_A );
173 _make_cat_compat( $category );
175 // Escape data pulled from DB.
176 $category = wp_slash($category);
178 // Merge old and new fields with new fields overwriting old ones.
179 $catarr = array_merge($category, $catarr);
181 return wp_insert_category($catarr);
189 * {@internal Missing Short Description}}
193 * @param int|string $tag_name
196 function tag_exists($tag_name) {
197 return term_exists($tag_name, 'post_tag');
201 * {@internal Missing Short Description}}
205 * @param int|string $tag_name
206 * @return array|WP_Error
208 function wp_create_tag($tag_name) {
209 return wp_create_term( $tag_name, 'post_tag');
213 * {@internal Missing Short Description}}
217 * @param int $post_id
218 * @return string|bool|WP_Error
220 function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
221 return get_terms_to_edit( $post_id, $taxonomy);
225 * {@internal Missing Short Description}}
229 * @param int $post_id
230 * @return string|bool|WP_Error
232 function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
233 $post_id = (int) $post_id;
237 $terms = get_object_term_cache( $post_id, $taxonomy );
238 if ( false === $terms ) {
239 $terms = wp_get_object_terms( $post_id, $taxonomy );
240 wp_cache_add( $post_id, $terms, $taxonomy . '_relationships' );
246 if ( is_wp_error( $terms ) ) {
249 $term_names = array();
250 foreach ( $terms as $term ) {
251 $term_names[] = $term->name;
254 $terms_to_edit = esc_attr( join( ',', $term_names ) );
257 * Filter the comma-separated list of terms available to edit.
261 * @see get_terms_to_edit()
263 * @param array $terms_to_edit An array of terms.
264 * @param string $taxonomy The taxonomy for which to retrieve terms. Default 'post_tag'.
266 $terms_to_edit = apply_filters( 'terms_to_edit', $terms_to_edit, $taxonomy );
268 return $terms_to_edit;
272 * {@internal Missing Short Description}}
276 * @param int|string $tag_name
277 * @return array|WP_Error
279 function wp_create_term($tag_name, $taxonomy = 'post_tag') {
280 if ( $id = term_exists($tag_name, $taxonomy) )
283 return wp_insert_term($tag_name, $taxonomy);