Importing Categories in WordPress: A Simple and Effective Solution

Importing Categories in WordPress: A Simple and Effective Solution

Introduction:

Organizing content efficiently is crucial for any blog or website. Categories play a significant role in this by helping readers navigate through different topics. However, importing a large number of categories into WordPress can be challenging. I recently faced this issue and tried several out-of-the-box solutions. Here’s how I overcame the problem with a simple tweak that made my life easier.

One step at a time. You'll get there!

The Problem:

I needed to import a large number of categories into my WordPress site. Doing this manually was not an option due to the sheer volume. Naturally, I turned to plugins that promised to handle CSV imports effortlessly. However, I quickly encountered several roadblocks:

  • Some plugins failed to upload the CSV files correctly.
  • Others required paid versions to access CSV import features.

Possible Out-of-the-Box Solutions:

  1. WP All Import: Initially, I tried WP All Import, a popular plugin that handles various import tasks. Unfortunately, the free version doesn’t support taxonomy imports, which includes categories.
  2. WP Ultimate CSV Importer: Next, I looked into WP Ultimate CSV Importer. While it’s a robust tool, the CSV import feature for taxonomies is locked behind a paywall.

These limitations prompted me to think outside the box. I needed a solution that didn’t rely on paid plugins or cumbersome workarounds.

The Solution:

After some research and a bit of experimentation, I devised a simple yet effective solution. The idea was to convert my categories CSV into a PHP script, add it to my theme’s functions.php file, and execute it by opening a page. Here’s how I did it:add it to my theme’s functions.php file, and execute it by opening a page. Here’s how I did it:

  1. Convert CSV to Script: First, I prepared a list of categories and subcategories in an array format. Here’s an example with three main categories and three subcategories in each:
function create_blog_categories() {
    $categories = [
        ['name' => 'Technology', 'slug' => 'technology', 'description' => 'All about technology', 'parent' => ''],
        ['name' => 'AI and Machine Learning', 'slug' => 'ai-machine-learning', 'description' => 'Advancements in AI and ML', 'parent' => 'Technology'],
        ['name' => 'Blockchain', 'slug' => 'blockchain', 'description' => 'Blockchain technology and its applications', 'parent' => 'Technology'],
        ['name' => 'Health', 'slug' => 'health', 'description' => 'Health and wellness tips', 'parent' => ''],
        ['name' => 'Mental Health', 'slug' => 'mental-health', 'description' => 'Mental health awareness and tips', 'parent' => 'Health'],
        ['name' => 'Physical Health', 'slug' => 'physical-health', 'description' => 'Physical health and fitness', 'parent' => 'Health'],
        ['name' => 'Travel', 'slug' => 'travel', 'description' => 'Travel tips and destination guides', 'parent' => ''],
        ['name' => 'Travel Tips', 'slug' => 'travel-tips', 'description' => 'Tips for a better travel experience', 'parent' => 'Travel'],
        ['name' => 'Destination Guides', 'slug' => 'destination-guides', 'description' => 'Guides to various travel destinations', 'parent' => 'Travel']
    ];

    foreach ($categories as $category) {
        $parent_id = 0;
        if (!empty($category['parent'])) {
            $parent = get_term_by('name', $category['parent'], 'category');
            $parent_id = $parent ? $parent->term_id : 0;
        }

        if (!term_exists($category['slug'], 'category')) {
            wp_insert_term(
                $category['name'],
                'category',
                [
                    'description' => $category['description'],
                    'slug' => $category['slug'],
                    'parent' => $parent_id
                ]
            );
        }
    }
}
// Uncomment the line below to run the function once
// create_blog_categories();
  1. Add the Script to functions.php: I added this script to my theme’s functions.php file. Here’s how you can do it:
    • Go to Appearance > Theme Editor.
    • Open functions.php.
    • Paste the script at the end of the file.
    • Uncomment the line // create_blog_categories(); to run the function.
  2. Run the Script: To execute the script, I simply loaded any page of my WordPress site. This created the categories and subcategories as specified in the array.
  3. Comment Out the Script: After the categories were created, I commented out the line create_blog_categories(); to prevent the function from running on every page load.

Takeaway:

By converting the CSV data into a script and running it through functions.php, I was able to import categories into WordPress without relying on paid plugins. This simple tweak saved me a lot of time and effort, and it can do the same for you. If you’re facing similar issues, give this method a try and see how easy it can be to manage your WordPress categories.

Feel free to adjust the categories and subcategories as per your needs, and happy blogging!

Posted in Guides and Tutorials, How-To GuidesTags:
Write a comment