Created
August 26, 2014 15:55
-
-
Save ejdanderson/5b1e99a11a6a58990c1c to your computer and use it in GitHub Desktop.
Programatically create taxonomy terms
This file contains hidden or 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 | |
// 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