Skip to content

Instantly share code, notes, and snippets.

@morgyface
Created November 25, 2020 17:21
Show Gist options
  • Save morgyface/eb10302816fab57704b9a866c8fd69cd to your computer and use it in GitHub Desktop.
Save morgyface/eb10302816fab57704b9a866c8fd69cd to your computer and use it in GitHub Desktop.
WordPress | Add categories programmatically on theme activation
<?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