Skip to content

Instantly share code, notes, and snippets.

@ejdanderson
Created August 26, 2014 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ejdanderson/5b1e99a11a6a58990c1c to your computer and use it in GitHub Desktop.
Save ejdanderson/5b1e99a11a6a58990c1c to your computer and use it in GitHub Desktop.
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