Skip to content

Instantly share code, notes, and snippets.

@ejdanderson
Created August 26, 2014 15:55
Embed
What would you like to do?
Programatically create taxonomy terms
<?php
// Prevents duplicate term creation, useful when terms need to be programatically generated
function create_term( $slug, $name, $taxonomy ) {
$term_id = term_exists( $slug, $taxonomy );
if ( ! $term_id ) {
$term_data = wp_insert_term( $name, $taxonomy, array( 'slug' => $slug ) );
if ( $term_data && ! is_wp_error( $term_data ) ) {
$term_id = $term_data['term_id'];
}
}
else {
$term_id = $term_id['term_id'];
}
return $term_id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment