Created
November 25, 2020 17:21
-
-
Save morgyface/eb10302816fab57704b9a866c8fd69cd to your computer and use it in GitHub Desktop.
WordPress | Add categories programmatically on theme activation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Sets the default categories | |
function add_the_categories() { | |
$categories = array( | |
'Case Study', | |
'Blog', | |
'Article' | |
); | |
foreach( $categories as $category ) { | |
$exists = term_exists( $category, 'category' ); | |
if( !$exists ) { | |
// Insert the term if it doesn't exist | |
$term = wp_insert_term( | |
$category, // the term | |
'category', // the taxonomy | |
array( | |
'slug' => sanitize_title( $category ) | |
) | |
); | |
} | |
} | |
} | |
add_action( 'after_switch_theme', 'add_the_categories' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment