Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Check the line; list($term_id,$taxonomy_id) = $result in the code snippet below. ( towards the bottom ). Why am I missing the term_id & the $taxonomy_id when I echo their values? Aren't they supposed to be populated by the wp_insert them?

$term_name = 'Uncategorized';
$term_slug = sanitize_title('Uncategorized', 'Default category slug');
$term_taxonomy = 'category';
$term_description = $term_slug . " desc here.. ";
$term_parentID = 0;

if ( is_term( $term_name , $term_taxonomy ) ):
    echo "this term exists!";
    die;
else:
    //echo "This terms does not exist!";
    //echo "<pre>";
    //global $wp_taxonomies;
    //print_r ($wp_taxonomies);
    //echo "</pre>";
    if (taxonomy_exists($term_taxonomy)):
        //insert the term into the wp_terms
        $args = array(
                            'description' => $term_description,
                            'slug' => $term_slug,
                            'parent' => $term_parentID 
                        );  

        $result = wp_insert_term($term_name, $term_taxonomy, $args);

        if ( is_wp_error( $result ) ):
            $error_string = $result->get_error_message();
            echo '<div id="message" class="error"><p>' . $error_string . '</p> </div>';
        else:
            list($term_id,$taxonomy_id) = $result;
            echo "<p>The term <i>{$term_name}</i> under the <i>{$term_taxonomy}</i> taxonomy has been added into the <i>wp_terms & wp_term_taxonomy<i> tables. The Term ID is {$term_id} and the related taxonomy ID is {$term_taxonomy_id}.";
            //outputs as The term Uncategorized under the category taxonomy has been added into the wp_terms & wp_term_taxonomy tables. The Term ID is and the related taxonomy ID is .
        endif;                          
    else:
        echo "<p>{$term_taxonomy} does not exist. we cannot add this $term. Please use the register_taxonomy function to register this taxonomy. ";
    endif;
endif;

share|improve this question
1  
You should use term_exists rather than the depreciated is_term. –  Stephen Harris Apr 11 '12 at 0:29
    
I just ran the above code and it works fine: I get 'this term exists' (as expected). –  Stephen Harris Apr 11 '12 at 0:31
    
But do you get the term_id of the term that exists? Just echo the $term_ID. I get blank. My echo outputs this; //outputs as The term Uncategorized under the category taxonomy has been added into the wp_terms & wp_term_taxonomy tables. The Term ID is and the related taxonomy ID is . –  Average Joe Apr 11 '12 at 0:37
    
Interestingly, when I did print_r on $results, I'm seeing Array ( [term_id] => 1 [term_taxonomy_id] => 1 ) But then why does list() function is failing to assign the term_id into $term_id? –  Average Joe Apr 11 '12 at 0:40
add comment

2 Answers

up vote 1 down vote accepted

The problem is the use of the php function list. If successful, wp_insert_term returns something of the form:

array(2) { ["term_id"]=> int(307) ["term_taxonomy_id"]=> int(325) }

You can't use list with this as list only works for numerical arrays. Instead try extract instead:

extract($result);
echo "<p>The term <i>{$term_name}</i> under the <i>{$term_taxonomy}</i> taxonomy has been added into the <i>wp_terms & wp_term_taxonomy<i> tables. The Term ID is {$term_id} and the related taxonomy ID is {$term_taxonomy_id}.";
share|improve this answer
    
You are right on the dot. Thank you! –  Average Joe Apr 11 '12 at 0:43
    
I wrapped the whole thing into a library function. –  Average Joe Apr 11 '12 at 1:36
add comment
//------get term/taxonomy data

$term_name = 'Uncategorized';
$term_slug = 'Default category slug';  //this gets sanitized by the blp_wordpress_insert_term function!
$term_taxonomy = 'category';
$term_description = $term_name . " desc here.. ";
$term_parentID = 0;

//------byref stuff: library function will populate them in return

$term_id = null; 
$term_taxonomy_id = null; 
$term_error = null; 

//------call the library function

if (blp_wordpress_insert_term($term_name,$term_slug,$term_taxonomy,$term_description,$term_parentID,$term_id,$term_taxonomy_id,$term_error)):
    echo "<p>" , "The term <i>{$term_name}</i> under the <i>{$term_taxonomy}</i> taxonomy has been added into the <i>wp_terms & wp_term_taxonomy<i> tables. The Term ID is {$term_id} and the related taxonomy ID is {$term_taxonomy_id}.";
else:
    echo "<p>" , $term_error;
    die;
endif;

//-----------------------------here is the library function

function blp_wordpress_insert_term($term_name,$term_slug,$term_taxonomy,$term_description,$term_parentID,&$term_id,&$term_taxonomy_id,&$term_error){

    /*

    inserts a term under the given taxonomy and returns the term_id and term_taxonomy_id byref if everything is cool/

    it also returns as TRUE or FALSE as a whole depending on a proper entry

    it also populates the $term_error variable - passed byref if an error occurs

    TO DO: validate the passed parentid to see if its a valid ID 

    */

    $term_slug = sanitize_title($term_name, $term_slug);

    if ( term_exists( $term_name , $term_taxonomy ) ):
        $term_error = "ERROR: The term ({$term_name}) already exists!";
        return false;

    else:

        if (taxonomy_exists($term_taxonomy)): //insert the term into the wp_terms

            $args = array(
                                'description' => $term_description,
                                'slug' => $term_slug,
                                'parent' => $term_parentID 
                            );  

            $result = wp_insert_term($term_name, $term_taxonomy, $args);
            //print_r ($result); // Array ( [term_id] => 1 [term_taxonomy_id] => 1 )

            if ( is_wp_error( $result ) ):
                $term_error = "ERROR: " . $result->get_error_message();
                return false;
            else:
                $term_id = $result['term_id'];
                $term_taxonomy_id = $result['term_taxonomy_id']; 
                return true;
            endif;                          
        else:
            $term_error = "ERROR: The taxonomy {$term_taxonomy} does not exist. blp_wordpress_insert_term cannot add a term when its associated taxonomy missing. Use the <i>register_taxonomy</i> function to register the taxonomy <i>{$term_taxonomy}. ";
            return false;
        endif;
    endif;

}

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.