Title: wp_insert_term not working
Last modified: May 13, 2022

---

# wp_insert_term not working

 *  Resolved [Bubblechaz](https://wordpress.org/support/users/bubblechaz/)
 * (@bubblechaz)
 * [4 years ago](https://wordpress.org/support/topic/wp_insert_term-not-working/)
 * I have a form which allows certain roles to add custom terms to a taxonomy.
 * My taxonomy is called Genre (slug genre)
 * <form method=’post’ enctype=’multipart/form-data’>
    <input type=”text” value=””
   name=”new”> <input type=’submit’ name=’Save’> </form> I have been going around
   in circles with this for hours. Ive searched for other questions similar and 
   have tried the suggestions.
 * I am using Custom Post Type UI to create the custom taxonomies – Was wondering
   maybe my script is firing before this plugin does and therefore wp_insert_terms
   happens before the register of the custom taxonomy?
 * My PHP:
 * if(isset($_POST[‘save’]) && !empty($_POST)) {
 *  $term = ($_POST[‘new’]);
 *  $existent_term = term_exists( $term, ‘genre’ );
 *  if( $existent_term && isset($existent_term[‘term_id’]) ) {
 *  $term_id = $existent_term[‘term_id’];
 *  } else {
 *  //insert the term if it doesn’t exsit
    $term = wp_insert_term( $term, // the
   term ‘genre’ // the taxonomy ); if( !is_wp_error($term ) && isset($term[‘term_id’])){
   $term_id = $term[‘term_id’]; }
 *  }
 * } //end isset
    Nothing shows in the error log either.
 * Has anyone come across this issue before? or is anyone able the assist please
 * Ive tried things like setting $term = ($_POST[‘new’]); to $term = “music”; and
   having the input value like <input type=”text” value=”music” name=”new”>
 * Also I have tried with an array
 * wp_insert_term(
    $term, // the term ‘genre’, // the taxonomy array( ‘description’
   => ‘I am description.’, ‘slug’ => ‘music’,
 *  )
    );`

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [4 years ago](https://wordpress.org/support/topic/wp_insert_term-not-working/#post-15643272)
 * Probably my first and biggest question is if you have this code to try and save
   the information running directly in your functions.php file. Like the version
   below, imagine being the very top of that functions.php file:
 *     ```
       <?php
       if(isset($_POST[‘save’]) && !empty($_POST)) {
        ...
       }
       ```
   
 * Or do you have it all in a callback on something like the `init` hook?
 *     ```
       function bubblechaz_create_terms_on_submission() {
           if(isset($_POST[‘save’]) && !empty($_POST)) {
               ...
           }
       }
       add_action( 'init', 'bubblechaz_create_terms_on_submission' );
       ```
   
 * I’d honestly and personally recommend the latter because the first example is
   running very early all in all, and things definitely may not be set up completely.
 * I know that we register our taxonomies on `init` priority 9, so anything after
   that, including my example above (which runs on priority 10), should help out.
 * Worth a test.
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [4 years ago](https://wordpress.org/support/topic/wp_insert_term-not-working/#post-15650799)
 * Any news or changes on this one?

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wp_insert_term not working’ is closed to new replies.

 * ![](https://ps.w.org/custom-post-type-ui/assets/icon-256x256.png?rev=2744389)
 * [Custom Post Type UI](https://wordpress.org/plugins/custom-post-type-ui/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-post-type-ui/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-post-type-ui/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-post-type-ui/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-post-type-ui/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-post-type-ui/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * Last activity: [4 years ago](https://wordpress.org/support/topic/wp_insert_term-not-working/#post-15650799)
 * Status: resolved