• Resolved Bubblechaz

    (@bubblechaz)


    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

    (@tw2113)

    The BenchPresser

    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

    (@tw2113)

    The BenchPresser

    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.