Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: Limit tags to a pre-defined list?

@super-powered

Participant

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');

Skip to toolbar