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.

I am building a front-end post edit / create form. The form uses an auto suggest for taxonomy, powered by built in suggest script & the ajax-tag-search action. The script is working fine, but I am bit confused on the processing.

This is the JavaScript function:

if($('#sponsorship-suggest')){
    $('#sponsorship-suggest').suggest(userSettings.ajaxurl+"?action=ajax-tag-search&tax=sponsorship-term",{
        multiple:false,
        multipleSep: ","
    });
}

The auto suggest outputs the term name only, just like the one on /wp-admin/.

This is how currently I save the term.

  1. Check if a value is submitted
  2. Check if the submitted value is a valid term
  3. If yes, get the term object
  4. If it is different than the previous value, set as object term.

    if ( isset($_POST['sponsorship-term']) && $_POST['sponsorship-term'] != '') {
        // check if it is a legit term
        $new_term = get_term_by( 'name', $_POST['sponsorship-term'], 'sponsorship-term');
        if (!$new_term) {
            $result['type'] = 'error';
            $result['message'] = 'Sponsorship is not registered';
            ajax_response($result,$return); // this is a function to set the response value
        }
        // update term
        $old_sponsorship = wp_get_object_terms($lot_id, 'sponsorship-term');
        if ($old_sponsorship->term_id != $new_term->term_id) {
            $change = wp_set_object_terms($lot_id, intval($new_term->term_id), 'sponsorship-term',FALSE );
            if (is_wp_error( $change )) {
                wp_die( 'Error saving term.' );
            }
        }
    }
    

The process works fine, but I'm curious about how they do it in the wp-admin.

Question

How can I make the process more efficient?

Is there a built in function to do that?

share|improve this question
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.