Reply To: Limit tags to a pre-defined list?
The reason the function doesn’t work is because BBPress throws a fatal error when it tries to handle the array of items when it expects it as a string. We probably just need to get a filter in the core that allows us to manipulate that value before it tries to use the split() function.
Until then, kind of a hacky workaround is to use JS to append the values to a hidden field:
function display_tag_elements($selectedTags, $userRole, $task)
{
$html = array();
$tags = get_categories(array('hide_empty' => 0, 'taxonomy' => 'topic-tag'));
$selectedTagsArray = explode(', ', $selectedTags);
if ($userRole != 'bbp_participant' || $task != 'edit')
{
$html[] = "";
foreach ($tags as $tag)
{
$selected = '';
if (in_array($tag->name, $selectedTagsArray))
{
$selected = 'checked';
}
$html[] = "<input type='checkbox' class='bbp_topic_tags_checkbox' name='bbp_topic_tags_checkbox[]'" . $selected . " value='" . $tag->name . "'>" . $tag->name . " ";
}
$html[] = "<input type='hidden' class='bbp_topic_tags_hidden' name='bbp_topic_tags' value='".$selectedTags."'>";
$html[] = "
<script>
var checkboxes = document.getElementsByClassName('bbp_topic_tags_checkbox');
var hidden = document.getElementsByClassName('bbp_topic_tags_hidden');
var hiddenVal = hidden[0].value.split(', ');
for(var x = 0; x < checkboxes.length; x++)
{
checkboxes[x].addEventListener( 'change', function()
{
var index = hiddenVal.indexOf(this.value);
var checked = this.checked;
if(checked && !index > -1)
hiddenVal.push(this.value);
else if (!checked && index > -1)
hiddenVal.splice(index, 1);
hidden[0].value = hiddenVal.join();
});
}
</script>
";
} else
{
$html[] = $selectedTags;
}
return implode('', $html);
}
However this has a pretty notable downside in that someone could just inspect the code and add whatever they want to the hidden field. So you’ll want to make sure your also using one of the spam filters like bbp_new_reply_pre_set_terms to make sure the new values are terms you’ve predefined.
Side note: there also seems to be a pretty annoying bug in that participants can edit the topics tags on reply, so I would just disable it on reply for non-moderators.
EDIT: kind of like I just did. I don’t think I should be allowed to add tags to existing topics. this feels like a bug.