I have been using a chunk of code as a plugin that was suggested in the answer to this question. Please check the code provided below instead of the one in the question, as I may have modified it a bit. The plugin works fine for the normal post type but has an issue when used with custom-post-type. My custom post type is called "research".
To be explicit, the code is not able to save the tag/term assigned with checkboxes, when used in the single-template of custom post type "research". However it does save the tags assigned with checkboxes, when used in the single-template of the normal-post-type. I would really appreciate the correction in the code please.
FRONTEND TAGGING PLUGIN CODE :
add_action('init', 'frontend_term_assignment');
add_action('frontend_term_assignment_form', 'frontend_term_assignment_form');
function frontend_term_assignment() {
if (
! isset($_POST['term_assignment_post_id']) ||
! intval($_POST['term_assignment_post_id']) ||
! current_user_can('manage_options', $_POST['term_assignment_post_id']) ||
! isset( $_POST['term_assignment_nonce'] ) ||
! wp_verify_nonce($_POST['term_assignment_nonce'], 'frontend_term_assignment')
) return;
$post = get_post( $_POST['term_assignment_post_id'] );
$taxonomy = null;
if ( $post->post_type == 'post' ) {
$taxonomy = 'post_tag';
} elseif ( $post->post_type == 'research' ) {
$taxonomy = 'research_sections';
}
if ( empty($taxonomy) || ! isset($_POST[$taxonomy]) ) return;
$terms = $_POST[$taxonomy];
wp_set_post_terms( $post->ID, $terms, $taxonomy, false);
}
function frontend_term_assignment_form() {
$post = get_queried_object();
if ( empty($post) || ! isset($post->ID) || ! current_user_can('manage_options', $post->ID) )
$taxonomy = null;
if ( $post->post_type == 'post' ) {
$taxonomy = 'post_tag';
} elseif ( $post->post_type == 'research' ) {
$taxonomy = 'research_sections';
}
if ( empty($taxonomy) ) return;
$tax_obj = get_taxonomy($taxonomy);
$args = array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false );
$terms = get_terms($taxonomy, $args);
$post_terms = (array)wp_get_post_terms( $post->ID, $taxonomy, array("fields" => "ids") );
if ( ! empty($terms) ) {
echo '<form method="post" id="frontend_term_assignment_form">';
echo '<input type="hidden" value="' . wp_create_nonce('frontend_term_assignment') . '" name="term_assignment_nonce">';
echo '<input type="hidden" value="' . $post->ID . '" name="term_assignment_post_id">';
echo '<div class="' . $taxonomy . '_term_list"><ul>';
foreach ($terms as $term ) {
$checked = in_array($term->term_id, $post_terms) ? checked(1, 1, 0) : '';
printf('<li><label><input type="checkbox" value="%s" name="%s[]"%s> ' . $term->name . '</label></li>', $term->slug, $taxonomy, $checked);
}
echo '</ul><input type="submit" id="tag-sub" value="' . esc_attr( sprintf(__('Save %s'), $tax_obj->label) ) . '" />';
echo '</div></form>';
}
}
ADDED NOTES : Below given is a the code that is used in the single.php templates which displays the form. Tags if assigned from the wp-backend works fine and is saved fine, the trouble is just saving tags in the frontend.
<?php do_action('frontend_term_assignment_form'); ?>
add_action('frontend_term_assignment_form', 'frontend_term_assignment_form');
?<?php do_action('frontend_term_assignment_form'); ?>
.