I tried modifying the code for my custom taxonomy of “country”, and although it showed the dropdown box, it wouldn’t save the data. What did I do wrong? I’m not using custom post types.
function wpufe_country( $post_type, $post = null) {
?>
<label for="country">
Media <span class="required">*</span>
</label>
<?php
// Add security nonce check
wp_nonce_field( __FILE__, 'nonce-country' );
// Get all media taxonomy terms
$medias = get_terms('country', 'hide_empty=0'); //$medias = wp_get_object_terms( $post->ID, 'country', array('fields' => 'ids') );
?>
<li>
<?php
// You can also use the included dropdown generator function of wordpress, but I'd prefer to code the select myself in order to avoid possible problems
//wp_dropdown_categories('taxonomy=country&hide_empty=0&orderby=name&name=post_media&show_option_none=Select media&selected='.$medias[0]);
?>
<select name='post_media' id='post_media' class="requiredField">
<option value='' <?php if (!count($medias)) echo "selected='selected'";?>>Select a Country</option>
<?php
foreach ($medias as $media) {
$selected = (has_term($media->slug, 'country', $post->ID)) ? ' selected="selected" ' : '';
echo "<option value='" . $media->name . "' " . $selected . ">" . $media->name . "</option>\n";
}
?>
</select>
</li>
<?php
}
/**
* The following action adds a form section
* below the description field of WP User Frontend
*/
add_action( 'wpuf_add_post_form_after_description', 'wpufe_country', 10, 2 );
/**
* Validate existence of the media after anecdote creation/edit
* If the select is empty, it returns an error message
*
* @uses 'wpuf_add_post_validation' filter
*
* @param array $errors errors array
* @return array errors array
*/
function wpufe_media_validation( $errors ) {
if( $_POST['post_media'] == '' ) {
$errors[] = 'Please select a country';
}
return $errors;
}
add_filter( 'wpuf_add_post_validation', 'wpufe_media_validation' );
add_filter( 'wpuf_edit_post_validation', 'wpufe_media_validation' );
/**
* Input the media data after submitting
*/
if (function_exists('wpufe_country')) {
add_action('save_post', 'save_country_data');
}
/**
* Save the media taxonomy data
*/
function save_country_data($post_id) {
// verify this came from our screen and with proper authorization.
if ( !wp_verify_nonce( $_POST['nonce-country'], __FILE__ )) {
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
// maybe you'll find this unnecessary since there is no possible autosave in the frontend
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( 'anecdote' == $_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
$post = get_post($post_id);
if ($post->post_type == 'country') {
$media = $_POST['post_media'];
wp_set_object_terms( $post_id, $media, 'country' );
}
}