How to Use PHP Script to Create Categories in WordPress
Efficiently managing categories and tags in WordPress can be time-consuming, especially for large-scale content creation. With our custom script, you can automate the process of adding categories, descriptions, and tags programmatically. Here’s a step-by-step guide on how to use it.
Contents
When Should You Use PHP Script?
- If you’re setting up a new WordPress website and need predefined categories and tags.
- When you’re migrating or restructuring a website and want to ensure consistent taxonomy.
- For content-heavy sites, where multiple blog categories and tags are required for SEO optimization.
- To save time and reduce errors during manual entry of categories and tags.
How to Use the PHP Script
Follow these steps to implement and run the script:
Step 1: Copy the Script
Use the code below. These scripts are optimized to create categories with descriptions and tags with descriptions programmatically.
Use the script for categories only or with categories
Script For Creating Categories Only
<?php function create_blog_categories_with_descriptions_v1() { // Check if this script has already run to avoid duplicating categories and tags if (get_option('categories_with_descriptions_created_v1')) return; // Define categories with descriptions $categories = [ 'Technology Trends' => 'Stay updated with the latest trends in technology and innovation.', 'Health & Wellness' => 'Explore tips and insights for maintaining a healthy lifestyle.', 'Personal Finance' => 'Learn to manage your money effectively with expert advice.', 'Travel & Adventure' => 'Discover travel guides and tips to make your adventures memorable.', 'Food & Recipes' => 'Explore delicious recipes and food tips for everyday cooking.' ]; // Create categories with descriptions foreach ($categories as $category_name => $category_description) { if (!term_exists($category_name, 'category')) { wp_insert_term($category_name, 'category', [ 'description' => $category_description ]); } } // Mark the option to prevent re-running update_option('categories_with_descriptions_created_v1', true); } // Hook to run the function once WordPress initializes add_action('init', 'create_blog_categories_with_descriptions_v1'); ?>
Script For Creating Both Category and Tags
'Stay updated with the latest trends in technology and innovation.', 'Health & Wellness' => 'Explore tips and insights for maintaining a healthy lifestyle.', 'Personal Finance' => 'Learn to manage your money effectively with expert advice.', 'Travel & Adventure' => 'Discover travel guides and tips to make your adventures memorable.', 'Food & Recipes' => 'Explore delicious recipes and food tips for everyday cooking.' ]; // Define tags $tags = [ 'Tech Updates', 'Healthy Living', 'Money Management', 'Travel Guides', 'Easy Recipes' ]; // Create categories with descriptions foreach ($categories as $category_name => $category_description) { if (!term_exists($category_name, 'category')) { wp_insert_term($category_name, 'category', [ 'description' => $category_description ]); } } // Create tags foreach ($tags as $tag_name) { if (!term_exists($tag_name, 'post_tag')) { wp_insert_term($tag_name, 'post_tag'); } } // Mark the option to prevent re-running update_option('categories_and_tags_with_descriptions_created_v3', true); } // Hook to run the function once WordPress initializes add_action('init', 'create_blog_categories_and_tags_with_descriptions_v3'); ?>
Step 2: Add the Script to WordPress using any of the below methods
- Using the Theme Editor:
- Go to Appearance > Theme Editor.
- Open the
functions.php
file of your theme. - Paste the script at the end of the file and save it.
- Using a Snippet Plugin:
- Install and activate the WpCode plugin (or a similar plugin).
- Create a new snippet and paste the script into it (Make sure to change categories and descriptions as per your need).
- Set the snippet to run in the “Admin Only” location.
- Activate the snippet and reload your site once.
Step 3: Activate and Run
After activating the script, reload your WordPress dashboard to Run it.
The categories and tags will be created automatically. To reuse the script in the future:
- Deactivate the snippet or remove the script from
functions.php
. - Modify the categories, descriptions, and tags for a new set.
- Change the function name and option key (e.g.,
_v2
,_v3
) in all 4 places to prevent duplicates. - Activate the script again and reload your site.
Need Help?
If you encounter any issues or need assistance in implementing this script, feel free to contact us. Our team specializes in WordPress development and customization, and we’re here to help!
Streamline Your WordPress Workflow
Using this method saves time and ensures consistency in your content structure. Start automating your WordPress taxonomy management today!