WordPress:将多个checkbox-values保存到comment_meta

时间:2016-05-04 06:57:18

标签: php arrays database wordpress comments

我正在扩展评论字段以向评论添加标签。我想使用多个复选框输入,以便评论者可以选择任意数量的标签。

但是,我的代码只返回值" array"。如果我使用name='tags'(并删除[]),它只会将最后一个对象保存到数据库。

PHP:

//get tags and output the function as Html

add_action( 'comment_form_logged_in_after', 'fieldTags' );
     function fieldTags() {
        $tags = get_tags();
        $html = '<ul>';
        foreach ($tags as $tag){
             $tag_link = get_tag_link($tag->term_id);

             $html .= "<li class='form-tag'>";
             $html .= "<label for='{$tag->name}' class='tag-label'>{$tag->name}</label>";
             $html .= "<input id='tag-{$tag->name}' type='checkbox' class='input' name='tags[]' value='{$tag->name}'>";
             $html .= "</li>";
           }
           $html .= '</ul>';
             echo $html;
           }

    // Save the comment meta data along with comment

    add_action( 'comment_post', 'save_comment_meta_data' );

    function save_comment_meta_data( $comment_id ) {
        if ( ( isset( $_POST['tags'] ) ) && ( $_POST['tags'] != '') )
        $tags= wp_filter_nohtml_kses($_POST['tags']);
        add_comment_meta( $comment_id, 'tags', $tags );

    }

HTML输出:

    <ul>
      <li class='form-tag'>
        <label for='Construction' class='tag-label'>Construction</label>
        <input id='tag-Construction' type='checkbox' class='input' name='tags[]' value='Construction'>
      </li>
      <li class='form-tag'>
        <label for='Creative' class='tag-label'>Creative</label>
        <input id='tag-Creative' type='checkbox' class='input' name='tags[]' value='Creative'>
      </li>
      <li class='form-tag'>
        <label for='Integration' class='tag-label'>Integration</label>
        <input id='tag-Integration' type='checkbox' class='input' name='tags[]' value='Integration'>
      </li>
    </ul>

0 个答案:

没有答案