3 * WordPress Taxonomy Administration API.
6 * @subpackage Administration
14 * {@internal Missing Short Description}}
18 * @param unknown_type $cat_name
21 function category_exists($cat_name, $parent = 0) {
22 $id = term_exists($cat_name, 'category', $parent);
29 * {@internal Missing Short Description}}
33 * @param unknown_type $id
36 function get_category_to_edit( $id ) {
37 $category = get_category( $id, OBJECT, 'edit' );
42 * {@internal Missing Short Description}}
46 * @param unknown_type $cat_name
47 * @param unknown_type $parent
50 function wp_create_category( $cat_name, $parent = 0 ) {
51 if ( $id = category_exists($cat_name, $parent) )
54 return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
58 * {@internal Missing Short Description}}
62 * @param unknown_type $categories
63 * @param unknown_type $post_id
66 function wp_create_categories($categories, $post_id = '') {
68 foreach ($categories as $category) {
69 if ($id = category_exists($category))
72 if ($id = wp_create_category($category))
77 wp_set_post_categories($post_id, $cat_ids);
83 * Updates an existing Category or creates a new Category.
87 * @param mixed $catarr See defaults below. Set 'cat_ID' to a non-zero value to update an existing category. The 'taxonomy' key was added in 3.0.0.
88 * @param bool $wp_error Optional, since 2.5.0. Set this to true if the caller handles WP_Error return values.
89 * @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure, depending on param $wp_error.
91 function wp_insert_category($catarr, $wp_error = false) {
92 $cat_defaults = array('cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
93 $catarr = wp_parse_args($catarr, $cat_defaults);
94 extract($catarr, EXTR_SKIP);
96 if ( trim( $cat_name ) == '' ) {
100 return new WP_Error( 'cat_name', __('You did not enter a category name.') );
103 $cat_ID = (int) $cat_ID;
105 // Are we updating or creating?
106 if ( !empty ($cat_ID) )
112 $description = $category_description;
113 $slug = $category_nicename;
114 $parent = $category_parent;
116 $parent = (int) $parent;
120 if ( empty( $parent ) || ! term_exists( $parent, $taxonomy ) || ( $cat_ID && term_is_ancestor_of( $cat_ID, $parent, $taxonomy ) ) )
123 $args = compact('name', 'slug', 'parent', 'description');
126 $cat_ID = wp_update_term($cat_ID, $taxonomy, $args);
128 $cat_ID = wp_insert_term($cat_name, $taxonomy, $args);
130 if ( is_wp_error($cat_ID) ) {
137 return $cat_ID['term_id'];
141 * Aliases wp_insert_category() with minimal args.
143 * If you want to update only some fields of an existing category, call this
144 * function with only the new values set inside $catarr.
148 * @param array $catarr The 'cat_ID' value is required. All other keys are optional.
149 * @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
151 function wp_update_category($catarr) {
152 $cat_ID = (int) $catarr['cat_ID'];
154 if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
157 // First, get all of the original fields
158 $category = get_category($cat_ID, ARRAY_A);
160 // Escape data pulled from DB.
161 $category = wp_slash($category);
163 // Merge old and new fields with new fields overwriting old ones.
164 $catarr = array_merge($category, $catarr);
166 return wp_insert_category($catarr);
174 * {@internal Missing Short Description}}
178 * @param unknown_type $tag_name
181 function tag_exists($tag_name) {
182 return term_exists($tag_name, 'post_tag');
186 * {@internal Missing Short Description}}
190 * @param unknown_type $tag_name
193 function wp_create_tag($tag_name) {
194 return wp_create_term( $tag_name, 'post_tag');
198 * {@internal Missing Short Description}}
202 * @param unknown_type $post_id
205 function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
206 return get_terms_to_edit( $post_id, $taxonomy);
210 * {@internal Missing Short Description}}
214 * @param unknown_type $post_id
217 function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
218 $post_id = (int) $post_id;
222 $tags = wp_get_post_terms($post_id, $taxonomy, array());
227 if ( is_wp_error($tags) )
230 foreach ( $tags as $tag )
231 $tag_names[] = $tag->name;
232 $tags_to_edit = join( ',', $tag_names );
233 $tags_to_edit = esc_attr( $tags_to_edit );
234 $tags_to_edit = apply_filters( 'terms_to_edit', $tags_to_edit, $taxonomy );
236 return $tags_to_edit;
240 * {@internal Missing Short Description}}
244 * @param unknown_type $tag_name
247 function wp_create_term($tag_name, $taxonomy = 'post_tag') {
248 if ( $id = term_exists($tag_name, $taxonomy) )
251 return wp_insert_term($tag_name, $taxonomy);