3 * WordPress Categories to Tags Converter.
10 * WordPress categories to tags converter class.
12 * Will convert WordPress categories to tags, removing the category after the
13 * process is complete and updating all posts to switch to the tag.
17 class WP_Categories_to_Tags {
18 var $categories_to_convert = array();
19 var $all_categories = array();
20 var $tags_to_convert = array();
21 var $all_tags = array();
22 var $hybrids_ids = array();
25 echo '<div class="wrap">';
26 if ( ! current_user_can('manage_categories') ) {
27 echo '<div class="narrow">';
28 echo '<p>' . __('Cheatin’ uh?') . '</p>';
31 <div class="tablenav"><p style="margin:4px"><a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag"><?php _e( "Categories to Tags" ); ?></a>
32 <a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag&step=3"><?php _e( "Tags to Categories" ); ?></a></p></div>
40 function populate_cats() {
42 $categories = get_categories('get=all');
43 foreach ( $categories as $category ) {
44 $this->all_categories[] = $category;
45 if ( is_term( $category->slug, 'post_tag' ) )
46 $this->hybrids_ids[] = $category->term_id;
50 function populate_tags() {
52 $tags = get_terms( array('post_tag'), 'get=all' );
53 foreach ( $tags as $tag ) {
54 $this->all_tags[] = $tag;
55 if ( is_term( $tag->slug, 'category' ) )
56 $this->hybrids_ids[] = $tag->term_id;
60 function categories_tab() {
61 $this->populate_cats();
62 $cat_num = count($this->all_categories);
64 echo '<br class="clear" />';
68 echo '<h2>' . sprintf( _n( 'Convert Category to Tag.', 'Convert Categories (%d) to Tags.', $cat_num ), $cat_num ) . '</h2>';
69 echo '<div class="narrow">';
70 echo '<p>' . __('Hey there. Here you can selectively convert existing categories to tags. To get started, check the categories you wish to be converted, then click the Convert button.') . '</p>';
71 echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p></div>';
73 $this->categories_form();
75 echo '<p>'.__('You have no categories to convert!').'</p>';
79 function categories_form() { ?>
81 <script type="text/javascript">
83 var checkflag = "false";
84 function check_all_rows() {
85 field = document.catlist;
86 if ( 'false' == checkflag ) {
87 for ( i = 0; i < field.length; i++ ) {
88 if ( 'cats_to_convert[]' == field[i].name )
89 field[i].checked = true;
92 return '<?php _e('Uncheck All') ?>';
94 for ( i = 0; i < field.length; i++ ) {
95 if ( 'cats_to_convert[]' == field[i].name )
96 field[i].checked = false;
99 return '<?php _e('Check All') ?>';
105 <form name="catlist" id="catlist" action="admin.php?import=wp-cat2tag&step=2" method="post">
106 <p><input type="button" class="button-secondary" value="<?php esc_attr_e('Check All'); ?>" onclick="this.value=check_all_rows()" />
107 <?php wp_nonce_field('import-cat2tag'); ?></p>
108 <ul style="list-style:none">
110 <?php $hier = _get_term_hierarchy('category');
112 foreach ($this->all_categories as $category) {
113 $category = sanitize_term( $category, 'category', 'display' );
115 if ( (int) $category->parent == 0 ) { ?>
117 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php
119 if ( in_array( intval($category->term_id), $this->hybrids_ids ) )
120 echo ' <a href="#note"> * </a>';
122 if ( isset($hier[$category->term_id]) )
123 $this->_category_children($category, $hier); ?></li>
128 <?php if ( ! empty($this->hybrids_ids) )
129 echo '<p><a name="note"></a>' . __('* This category is also a tag. Converting it will add that tag to all posts that are currently in the category.') . '</p>'; ?>
131 <p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e('Convert Categories to Tags'); ?>" /></p>
136 function tags_tab() {
137 $this->populate_tags();
138 $tags_num = count($this->all_tags);
140 echo '<br class="clear" />';
142 if ( $tags_num > 0 ) {
144 echo '<h2>' . sprintf( _n( 'Convert Tag to Category.', 'Convert Tags (%d) to Categories.', $tags_num ), $tags_num ) . '</h2>';
145 echo '<div class="narrow">';
146 echo '<p>' . __('Here you can selectively convert existing tags to categories. To get started, check the tags you wish to be converted, then click the Convert button.') . '</p>';
147 echo '<p>' . __('The newly created categories will still be associated with the same posts.') . '</p></div>';
151 echo '<p>'.__('You have no tags to convert!').'</p>';
155 function tags_form() { ?>
157 <script type="text/javascript">
159 var checktags = "false";
160 function check_all_tagrows() {
161 field = document.taglist;
162 if ( 'false' == checktags ) {
163 for ( i = 0; i < field.length; i++ ) {
164 if ( 'tags_to_convert[]' == field[i].name )
165 field[i].checked = true;
168 return '<?php _e('Uncheck All') ?>';
170 for ( i = 0; i < field.length; i++ ) {
171 if ( 'tags_to_convert[]' == field[i].name )
172 field[i].checked = false;
175 return '<?php _e('Check All') ?>';
181 <form name="taglist" id="taglist" action="admin.php?import=wp-cat2tag&step=4" method="post">
182 <p><input type="button" class="button-secondary" value="<?php esc_attr_e('Check All'); ?>" onclick="this.value=check_all_tagrows()" />
183 <?php wp_nonce_field('import-cat2tag'); ?></p>
184 <ul style="list-style:none">
186 <?php foreach ( $this->all_tags as $tag ) { ?>
187 <li><label><input type="checkbox" name="tags_to_convert[]" value="<?php echo intval($tag->term_id); ?>" /> <?php echo esc_attr($tag->name) . ' (' . $tag->count . ')'; ?></label><?php if ( in_array( intval($tag->term_id), $this->hybrids_ids ) ) echo ' <a href="#note"> * </a>'; ?></li>
192 <?php if ( ! empty($this->hybrids_ids) )
193 echo '<p><a name="note"></a>' . __('* This tag is also a category. When converted, all posts associated with the tag will also be in the category.') . '</p>'; ?>
195 <p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php esc_attr_e('Convert Tags to Categories'); ?>" /></p>
200 function _category_children($parent, $hier) { ?>
202 <ul style="list-style:none">
203 <?php foreach ($hier[$parent->term_id] as $child_id) {
204 $child =& get_category($child_id); ?>
205 <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label><?php
207 if ( in_array( intval($child->term_id), $this->hybrids_ids ) )
208 echo ' <a href="#note"> * </a>';
210 if ( isset($hier[$child->term_id]) )
211 $this->_category_children($child, $hier); ?></li>
216 function _category_exists($cat_id) {
217 $cat_id = (int) $cat_id;
219 $maybe_exists = category_exists($cat_id);
221 if ( $maybe_exists ) {
228 function convert_categories() {
231 if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?>
233 <p><?php printf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag'); ?></p>
238 if ( empty($this->categories_to_convert) )
239 $this->categories_to_convert = $_POST['cats_to_convert'];
241 $hier = _get_term_hierarchy('category');
242 $hybrid_cats = $clear_parents = $parents = false;
243 $clean_term_cache = $clean_cat_cache = array();
244 $default_cat = get_option('default_category');
248 foreach ( (array) $this->categories_to_convert as $cat_id) {
249 $cat_id = (int) $cat_id;
251 if ( ! $this->_category_exists($cat_id) ) {
252 echo '<li>' . sprintf( __('Category %s doesn’t exist!'), $cat_id ) . "</li>\n";
254 $category =& get_category($cat_id);
255 echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... '), $category->name);
257 // If the category is the default, leave category in place and create tag.
258 if ( $default_cat == $category->term_id ) {
260 if ( ! ($id = is_term( $category->slug, 'post_tag' ) ) )
261 $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
263 $id = $id['term_taxonomy_id'];
264 $posts = get_objects_in_term($category->term_id, 'category');
267 foreach ( $posts as $post ) {
268 $values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order);
269 clean_post_cache($post);
273 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");
275 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $category->count, $category->term_id) );
278 echo __('Converted successfully.') . "</li>\n";
282 // if tag already exists, add it to all posts in the category
283 if ( $tag_ttid = $wpdb->get_var( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $category->term_id) ) ) {
284 $objects_ids = get_objects_in_term($category->term_id, 'category');
285 $tag_ttid = (int) $tag_ttid;
288 foreach ( $objects_ids as $object_id )
289 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tag_ttid, $term_order);
292 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");
294 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag_ttid) );
295 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $count, $category->term_id) );
297 echo __('Tag added to all posts in this category.') . " *</li>\n";
300 $clean_term_cache[] = $category->term_id;
301 $clean_cat_cache[] = $category->term_id;
306 $tt_ids = $wpdb->get_col( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) );
308 $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");
309 foreach ( (array) $posts as $post )
310 clean_post_cache($post);
313 // Change the category to a tag.
314 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) );
316 // Set all parents to 0 (root-level) if their parent was the converted tag
317 $parents = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id) );
319 if ( $parents ) $clear_parents = true;
320 $clean_cat_cache[] = $category->term_id;
321 echo __('Converted successfully.') . "</li>\n";
326 if ( ! empty($clean_term_cache) ) {
327 $clean_term_cache = array_unique(array_values($clean_term_cache));
328 clean_term_cache($clean_term_cache, 'post_tag');
331 if ( ! empty($clean_cat_cache) ) {
332 $clean_cat_cache = array_unique(array_values($clean_cat_cache));
333 clean_term_cache($clean_cat_cache, 'category');
336 if ( $clear_parents ) delete_option('category_children');
339 echo '<p>' . sprintf( __('* This category is also a tag. The converter has added that tag to all posts currently in the category. If you want to remove it, please confirm that all tags were added successfully, then delete it from the <a href="%s">Manage Categories</a> page.'), 'categories.php') . '</p>';
340 echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>';
343 function convert_tags() {
346 if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) {
347 echo '<div class="narrow">';
348 echo '<p>' . sprintf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&step=3') . '</p>';
353 if ( empty($this->tags_to_convert) )
354 $this->tags_to_convert = $_POST['tags_to_convert'];
356 $hybrid_tags = $clear_parents = false;
357 $clean_cat_cache = $clean_term_cache = array();
358 $default_cat = get_option('default_category');
361 foreach ( (array) $this->tags_to_convert as $tag_id) {
362 $tag_id = (int) $tag_id;
364 if ( $tag = get_term( $tag_id, 'post_tag' ) ) {
365 printf('<li>' . __('Converting tag <strong>%s</strong> ... '), $tag->name);
367 if ( $cat_ttid = $wpdb->get_var( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'category'", $tag->term_id) ) ) {
368 $objects_ids = get_objects_in_term($tag->term_id, 'post_tag');
369 $cat_ttid = (int) $cat_ttid;
372 foreach ( $objects_ids as $object_id ) {
373 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $cat_ttid, $term_order);
374 clean_post_cache($object_id);
378 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");
380 if ( $default_cat != $tag->term_id ) {
381 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag->term_id) );
382 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_id = %d AND taxonomy = 'category'", $count, $tag->term_id) );
387 $clean_term_cache[] = $tag->term_id;
388 $clean_cat_cache[] = $tag->term_id;
389 echo __('All posts were added to the category with the same name.') . " *</li>\n";
394 // Change the tag to a category.
395 $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) );
396 if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) {
398 $clear_parents = true;
400 $reset_parent = ", parent = '0'";
402 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) );
404 $clean_term_cache[] = $tag->term_id;
405 $clean_cat_cache[] = $cat['term_id'];
406 echo __('Converted successfully.') . "</li>\n";
409 printf( '<li>' . __('Tag #%s doesn’t exist!') . "</li>\n", $tag_id );
413 if ( ! empty($clean_term_cache) ) {
414 $clean_term_cache = array_unique(array_values($clean_term_cache));
415 clean_term_cache($clean_term_cache, 'post_tag');
418 if ( ! empty($clean_cat_cache) ) {
419 $clean_cat_cache = array_unique(array_values($clean_cat_cache));
420 clean_term_cache($clean_term_cache, 'category');
423 if ( $clear_parents ) delete_option('category_children');
427 echo '<p>' . sprintf( __('* This tag is also a category. The converter has added all posts from it to the category. If you want to remove it, please confirm that all posts were added successfully, then delete it from the <a href="%s">Manage Tags</a> page.'), 'edit-tags.php') . '</p>';
428 echo '<p>' . sprintf( __('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag&step=3' ) . '</p>';
433 $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
437 if ( current_user_can('manage_categories') ) {
441 $this->categories_tab();
445 check_admin_referer('import-cat2tag');
446 $this->convert_categories();
454 check_admin_referer('import-cat2tag');
455 $this->convert_tags();
463 function WP_Categories_to_Tags() {
468 $wp_cat2tag_importer = new WP_Categories_to_Tags();
470 register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init'));