Skip to:
Content
Pages
Categories
Search
Top
Bottom

Limit tags to a pre-defined list?

  • I’m wanting to limit the number of tags in my forum to a pre-defined list of 10 topics, and have members click/choose one (or more) when posting. Bonus points for having them in a drop-down menu… :)

    I saw someone wrote in a post, 8 months ago, “I hacked [my forum] so users would tick on tags that are pre-defined…” which is exactly what I want to do. Any ideas how to do that?

Viewing 12 replies - 26 through 37 (of 37 total)

  • jon
    Participant

    @jonathan-mangual

    I think is outdated, just did it on my bbpress and broke it. Everything is back to normal thx for the backups. Anyway my point is… is there someone who got it working?


    jeffacubed
    Participant

    @jeffacubed

    Wow – this whole thread is “exactly” what I’ve been wanting to accomplish. Did you ever get beyond

    But when I edit a topic with e.g. 3 tags, edit screen shows only 1 tag selected. After I save the edited topic, all tags are lost.

    @strategeek, @jonathan-mangual &/or @23creative?

    I’m soooo close – but equally stumped as to why all the tags vanish after an edit. -Jeff


    vanbinh97
    Participant

    @vanbinh97

    Seems there are not good solutions.


    super powered
    Participant

    @super-powered

    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 . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
    
            $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.


    super powered
    Participant

    @super-powered

    Relevant tag validation code for above since I had to write it for my project anyways:

    
    
    function new_reply_pre_set_terms_tag_test($terms, $topic_id, $reply_id)
    {
        //Bail Early if not set
        if(!$terms)
            return $terms;
    
        //Bail early if we are allowed to modify tags
        if(current_user_can( 'modify_topic_tags' ))
            return $terms;
    
        if(!is_array($terms))
            $terms = explode(', ', $terms);
    
        //For each to check if array term already exists. Remove if doesnt
        foreach ($terms as $tag => $tag_value):
            if(!term_exists($tag_value, 'topic-tag')):
                unset($terms[$tag]);
            endif;
        endforeach;
    
        return implode(', ', $terms);
    }
    
    function new_topic_pre_insert_tag_test($data)
    {
        //Bail Early
        if(!is_array($data) || !isset($data['tax_input']) || !isset($data['tax_input']['topic-tag']))
            return $data;
    
        //Bail early if we are allowed to modify tags
        if(current_user_can( 'modify_topic_tags' ))
            return $data;
    
        //Make sure we're an array
        if(!is_array($data['tax_input']['topic-tag']))
            $data['tax_input']['topic-tag'] = array($data['tax_input']['topic-tag']);
    
        //For each to check if array term already exists. Remove if doesnt
        foreach ($data['tax_input']['topic-tag'] as $tag => $tag_value):
            if(!term_exists($tag_value, 'topic-tag')):
                unset($data['tax_input']['topic-tag'][$tag]);
            endif;
        endforeach;
    
        return $data;
    }
    
    add_filter('bbp_new_reply_pre_set_terms','new_reply_pre_set_terms_tag_test', 22, 3);
    add_filter('bbp_edit_reply_pre_set_terms','new_reply_pre_set_terms_tag_test', 22, 3);
    add_filter('bbp_new_topic_pre_insert','new_topic_pre_insert_tag_test');
    
    

    jrgalia
    Participant

    @jrgalia

    Are there new workaround for this: to limit tags to a pre-defined list?


    Robin W
    Moderator

    @robin-w

    I’m working on a pre-defined list option at the moment – probably about a week and I’ll release some code


    pandex
    Participant

    @pandex

    onde eu insiro esse codigo? @strategeek


    pandex
    Participant

    @pandex

    are done? @ robin-w


    Robin W
    Moderator

    @robin-w

    The code is in my style pack.

    bbp style pack

    once activated go to

    dashboard>settings>bbp style pack>Topic/Reply Form

    and tick option 18.

    If this does not work, then it may be a theme or other plugin affecting


    pandex
    Participant

    @pandex

    @ robin-w I saw here that the problem is the Youzify plugin, when I disable it, it works! But the problem is that I need Youzify to run my social network! Is there anything I can do? Delete or include some PHP code?


    Robin W
    Moderator

    @robin-w

    There are 2 standard bbpress templates, one for the topic form, and one for the reply form.

    My plugin amends these templates, but I’d bet that so does the Yuozify plugin. Plugins tend to load in alphabetical order, so the Yuozify probably takes preference, but in any case, if we reversed it, my plugin would overwrite any changes they have made.

    I don’t have access to the Yuozify plugin as it is paid.

    Chances are that if we put a combined one in your child theme it would work, but this would be beyond free help, although not expensive.

    Maybe contact me via my contact page http://www.rewweb.co.uk/contact-me/ and I’ll see if I can help further

Viewing 12 replies - 26 through 37 (of 37 total)
  • You must be logged in to reply to this topic.
Skip to toolbar