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');