WP_Import::process_categories PHP Method

process_categories() public method

Doesn't create a new category if its slug already exists
public process_categories ( )
        function process_categories()
        {
            $this->categories = apply_filters('wp_import_categories', $this->categories);
            if (empty($this->categories)) {
                return;
            }
            foreach ($this->categories as $cat) {
                // if the category already exists leave it alone
                $term_id = term_exists($cat['category_nicename'], 'category');
                if ($term_id) {
                    if (is_array($term_id)) {
                        $term_id = $term_id['term_id'];
                    }
                    if (isset($cat['term_id'])) {
                        $this->processed_terms[intval($cat['term_id'])] = (int) $term_id;
                    }
                    continue;
                }
                $category_parent = empty($cat['category_parent']) ? 0 : category_exists($cat['category_parent']);
                $category_description = isset($cat['category_description']) ? $cat['category_description'] : '';
                $catarr = array('category_nicename' => $cat['category_nicename'], 'category_parent' => $category_parent, 'cat_name' => $cat['cat_name'], 'category_description' => $category_description);
                $id = wp_insert_category($catarr);
                if (!is_wp_error($id)) {
                    if (isset($cat['term_id'])) {
                        $this->processed_terms[intval($cat['term_id'])] = $id;
                    }
                } else {
                    printf(__('Failed to import category %s', 'wordpress-importer'), esc_html($cat['category_nicename']));
                    if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) {
                        echo ': ' . $id->get_error_message();
                    }
                    echo '<br />';
                    continue;
                }
            }
            unset($this->categories);
        }

Usage Example

 public function wpImportContents($file)
 {
     $wp_import = new WP_Import();
     $wp_import->fetch_attachments = false;
     // doesn matter
     // load data from saved option
     $wp_import->post_orphans = get_option('_cri_post_orphans', array());
     $wp_import->processed_posts = get_option('_cri_processed_posts', array());
     $wp_import->url_remap = get_option('_cri_url_remap', array());
     // the odd filter
     add_filter('import_post_meta_key', array($wp_import, 'is_valid_meta_key'));
     add_filter('http_request_timeout', array(&$wp_import, 'bump_request_timeout'));
     // start buffer
     ob_start();
     // parse file and gather data
     $wp_import->import_start($file);
     // map author
     $wp_import->get_author_mapping();
     wp_suspend_cache_invalidation(true);
     $wp_import->process_categories();
     $wp_import->process_tags();
     $wp_import->process_terms();
     $wp_import->process_posts();
     wp_suspend_cache_invalidation(false);
     // update incorrect/missing information in the DB
     $wp_import->backfill_parents();
     $wp_import->backfill_attachment_urls();
     $wp_import->remap_featured_images();
     // end has output, so buffer it out
     $wp_import->import_end();
     // ignore the output, call in buffer
     do_action('radium_after_content_import');
     ob_end_clean();
     // delete all attachment related stats
     foreach (array('_cri_post_orphans', '_cri_processed_posts', '_cri_url_remap') as $op) {
         delete_option($op);
     }
     return true;
 }
All Usage Examples Of WP_Import::process_categories