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 have a custom taxonomy, "Country". I am using WP User Frontend to let users post to the site. Most data is saved as custom fields, so I have a custom field "cf_country". I want to be able to set that custom field to the taxonomy for existing and all future posts. I found a converter plugin, but its a one time thing. I want this to happen automatically. Is there any way to do this?

Thanks!

Possible Solution `function save_taxonomy_data( $post_id ) {

// verify this came from our screen and with proper authorization.
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'wpuf-add-post' ) ) 
    return;

// OK, we're authenticated: we need to find and save the data
if ( isset( $_POST['cf_country'] ) ) { 
    wp_set_object_terms( $post_id, $_POST['cf_country'], 'country' );
}
}
add_action('wpuf_add_post_after_insert', 'save_taxonomy_data');
function edit_taxonomy_data( $post_id ) {

// verify this came from our screen and with proper authorization.
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'wpuf-edit-post' ) ) 
    return;

// OK, we're authenticated: we need to find and save the data
if ( isset( $_POST['cf_country'] ) ) { 
    wp_set_object_terms( $post_id, $_POST['cf_country'], 'country' );
}
}
add_action('save_post', 'edit_taxonomy_data');`
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Without knowing exactly how your input form looks, I can only give you the following as a suggestion. Basically you verify that you want/have permission to save the data and then take the meta's input and set it as a term in your taxonomy via wp_set_object_terms(). You'll want to fire this every time a post is saved... ie on the save_post hook.

function save_taxonomy_data($post_id) {

    // verify this came from our screen and with proper authorization.
    // your form should have a nonce <?php wp_nonce_field('taxonomy_country','taxonomy_noncename'); ?>
    if ( !wp_verify_nonce( $_POST['taxonomy_noncename'], 'taxonomy_country' )) {
        return $post_id;
    }

    // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;


    // Check permissions
    if ( 'page' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) )
            return $post_id;
    } else {
        if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;
    }

    // OK, we're authenticated: we need to find and save the data
    // right now it is only working on posts and pages, need to adapt to other types here
    $post = get_post($post_id);
    if ( ($post->post_type == 'post') || ($post->post_type == 'page') ) { 

        isset( $_POST['post_country'] )
            wp_set_object_terms( $post_id, $_POST['post_country'], 'country' );

    }

}
add_action('save_post', 'save_taxonomy_data');

Adapted from Custom Taxonomy Panels and also similar to Unable to save custom taxonomy terms in a custom-built metabox.

Second Attempt

The first attempt is pretty much how to save taxonomy data from a metabox. I took a glance at the WP User Frontend code and made a few adjustments. This is intended to replace what was above, and assumes that *cf_country* is the name element of the input.

function save_taxonomy_data( $post_id ) {

    // verify this came from our screen and with proper authorization.
    if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'wpuf-add-post' ) ) 
        return;

    // OK, we're authenticated: we need to find and save the data
    if ( isset( $_POST['cf_country'] ) ) { 
        wp_set_object_terms( $post_id, $_POST['cf_country'], 'country' );
    }

}
add_action('wpuf_add_post_after_insert', 'save_taxonomy_data');

Third Attempt:

function save_taxonomy_data($post_id) {

    /* 
     * verify this came from our screen and with proper authorization.
    * your form should have a nonce <?php wp_nonce_field('wpuf-add-post','_wpnonce'); ?>
    */
    if ( !wp_verify_nonce( $_POST['_wpnonce'], 'wpuf-add-post' )) {
        return $post_id;
    }

    // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;


    // Check permissions
    if ( 'page' == $_POST['wpuf_post_type'] ) { // post type is a hidden field in wpuf_
        if ( !current_user_can( 'edit_page', $post_id ) )
            return $post_id;
    } else {
        if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;
    }

    // Work on all post types
    if ( isset( $_POST['post_country'] ) ) 
        wp_set_object_terms( $post_id, $_POST['post_country'], 'country' );


}
add_action('save_post', 'save_taxonomy_data');
share|improve this answer
    
Here is the .php that controls the form input (pastebin.com/0VF0Q4tj). Hopefully that helps. As for your script, where would I add it? In my functions.php or elsewhere? I am unfamiliar with the save_post hook so a little guidance would be very appreciated! –  Acornrevolution Mar 1 '13 at 14:57
    
I added your script to my functions.php but it just stopped my site from working. Am I missing something? –  Acornrevolution Mar 2 '13 at 2:24
    
"Stopped your site from working"... how exactly? Enable wp_debug to get a useful message. I don't see any blatant punctuation errors, but that doesn't mean they aren't there. And this isn't guaranteed to work with your form specifically, it is more of a guideline on how to save taxonomy info. Finally looking at your code, there is this hook wpuf_add_post_after_insert which might be worth using in lieu of save_post. –  helgatheviking Mar 2 '13 at 4:48
    
Awesome! Thank you so much. It works, perfectly. It seems so simple, too. Thanks. –  Acornrevolution Mar 2 '13 at 6:31
    
Cool! Glad it worked, I Wasn't sure since I am not using your plugin and couldn't test it. –  helgatheviking Mar 2 '13 at 6:34
show 6 more comments

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.