Wordpress backend (post-editor-panel) offers such an excellent tagging tool wherein we can choose from already existing tags. Is it possible to bring this functionality to the frontend without fancy ajax. Just with some good ol php. All I need is just a list of tags to choose from in the frontend and save it. I will need the functionality for both post types, default wp post type as well as for custom-post-type.

There is one similar question with an unaccepted answer. I tried to use the code in the suggested answer, but it is far from perfect and just returns empty radio-buttons. There is nothing on google on this topic, Please suggest the correction in the code. Thanks.

$taxonomies = array( 
$args = array(
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => false

$terms = get_terms($taxonomies,$args);

if (count($terms) > 0):
$i = 0;
    foreach ($terms as $term): ?>
        <div class="wissen_tag_list">
            <input type="radio" value="<?php echo $term->term_id; ?>" name="wissen_tags" class="wissen_tag_list_ckb" <?php if ( $i == 0 ) { ?>checked<?php } ?>>
            <label class="wissen_tag_list_ckbl">
                <?php echo $term->name; ?>
    $i++; endforeach;
endif; ?>
  • @G.M. need help again, Your code works extremely well for normal-post-type but not for custom-post-type. On line 22, I changed the wp_set_post_terms to wp set object terms, this still allows me to save tags in normal-post-type but not in custom-post-type. Any inputs please. I have asked it as a new question here
    – gurung
    Dec 7, 2013 at 5:10
  • I've updated the code also changing wp_get_post_terms to wp_get_object_terms. Try now.
    – gmazzap
    Dec 7, 2013 at 6:51
  • @G.M. Still not saving the tags from the frontend, though it saves well in the backend while logged in as admin. If it matters, I have the code on single-research.php (single-template-for-custom-post-type) wrapped in a statement like this if(is_user_logged_in()) { do_action('frontend_term_assignment_form'); }. I noticed that you made changes in user capabilities, from manage_options to edit_posts so I tried changing it to others and also commenting it out. But the results are null. Could you please do an actual test and see it for yourself. Greatly appreciated as always.
    – gurung
    Dec 7, 2013 at 7:47

1 Answer 1


Notice: completely update code after OP update answer.

Assuming you want to update in front-end the taxonomy 'post_tag' (the standard tags) fro standard post and a taxonomy called 'research-sections' for a custom post type called 'research'.

Probably you have to put the form in both single.php and single-research.php and in both you have to some stuff... because is always better having a DRY code, and related to WP is always better keep templates clean, I suggest you to put all the functional code in a plugin or in functions.php and modify as less as possible the templates.

You need essentially 2 functions: one that output the form, another that saves data.

For the first you can create your own action, in this way in template only thing you need is a do_action call.

For the function that saves data you can hook a standard WP action, like init.

The 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('edit_posts', $_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_object_terms( $post->ID, $terms, $taxonomy, false);

function frontend_term_assignment_form() {
  $post = get_queried_object();
  if ( empty($post) || ! isset($post->ID) || ! current_user_can('edit_posts', $post->ID) ) return;
  $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_object_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" value="' . esc_attr( sprintf(__('Save %s'), $tax_obj->label) ) . '" />';
    echo '</div></form>';

I've added security check like check the current user capabilities and a nonce check.

Almost all work are done.

Now in the templates (both single.php and single-research.php) just add the do_action after your loop:

<?php while ( have_posts() ) : the_post(); ?>
  ...Your loop code here ...
<?php endwhile; ?>

// Nothing else is required here!

That's all. Hope it helps.

  • thank you for the response. yes I am trying to assign tags to single posts. I intend to wrap the solution code in if (current_user_can('manage_options', $post->ID)) to deny access to others. Please allow me a few minutes to test your solution.
    – gurung
    Sep 3, 2013 at 6:06
  • Unfortunately, the code throws a Syntax error, unexpected T_VARIABLE on line 3, which is $post_id = isset($the_post->ID) $the_post->ID : 0; ?
    – gurung
    Sep 3, 2013 at 6:10
  • ,now it throws Fatal error: Call to undefined function get_the_term() in /home2/site/public_html/wp-content/themes/theme/single.php on line 207. any ideas?
    – gurung
    Sep 3, 2013 at 7:01
  • Just so you know, eventually I aim to work with multiple tags. I was about to ask this later how to utilize this code for multiple tags. <input type="checkbox" value="<?php echo $term->term_id; ?>" name="wissen_tags[]" class="wissen_tag_list_ckb">
    – gurung
    Sep 3, 2013 at 7:10
  • sorry, but it still throws a different error Fatal error: Cannot use object of type WP_Error as array in /home2/site/public_html/wp-includes/functions.php on line 2688
    – gurung
    Sep 3, 2013 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.