23creative (@23creative)

Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)

  • 23creative
    Participant

    @23creative

    @strategeek

    its been a while and im not 100% sure this will work (in terms of sanitation checks) but if you change

    $html[] = '<select class="chzn-select" multiple="multiple" name="bbp_topic_tags[]" id="bbp_topic_tags">';
                foreach($tags as $tag){
                    $selected = '';
                    if (in_array($tag->name, $selectedTagsArray)) {
                       $selected = 'selected="selected"';
                    }
                    $html[] = '<option '.$selected.' value="'.$tag->name.'">'.$tag->name.'</option>';
                }
            $html[] = '</select>';

    to

    foreach($tags as $tag){
        $selected = '';
        if (in_array($tag->name, $selectedTagsArray)) {
          $selected = 'checked="selected"';
        }
       $html[] = '<input '.$selected.' type="checkbox" name="bbp_topic_tags" value="'.$tag->name.'">'.$tag->name;
    }

    i haven’t tested it but it should get you close


    23creative
    Participant

    @23creative

    Also in form-reply.php & form-topic.php template files (around line 68ish) you need to insert the following code. i would recommend setting up template overrides for this

    <?php if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags' ) ) : ?>
    
            <?php do_action( 'bbp_theme_before_reply_form_tags' ); ?>
            <p>
                <label for="bbp_topic_tags"><?php _e( 'Tags:', 'bbpress' ); ?></label><br />
                <?php  echo displayTagElements(bbp_get_form_topic_tags(), bbp_get_user_role(get_current_user_id()), 'edit'); ?>
            </p>
    
            <?php do_action( 'bbp_theme_after_reply_form_tags' ); ?>
    
    <?php endif; ?>

    23creative
    Participant

    @23creative

    you’re welcome mate. my code actually changed quite a bit form what I posted here. the tag list is pulled from wp-admin > Topics > Topic tags

    updated code below

    //change the seclected terms to string and return
    function restrict_topic_tags( $terms, $topic_id, $reply_id ) {
        if(isset($_POST['bbp_topic_tags'])){
            $bbp_topic_tags = filter_input(INPUT_POST, 'bbp_topic_tags', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
            return returnFormattedTags($bbp_topic_tags, $topic_id);
        }else{
            return $terms;
        }   
    }
    add_filter( 'bbp_new_reply_pre_set_terms', 'restrict_topic_tags' );
    
    //change the seclected terms to string and return
    function restrict_new_topic_tags( $topic_data ) {
       //$bbp_topic_tags = $data   = filter_input(INPUT_POST, 'bbp_topic_tags', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
       if(isset($_POST['bbp_topic_tags'])){
            $bbp_topic_tags = filter_input(INPUT_POST, 'bbp_topic_tags', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
            $topic_data['tax_input'] = array('topic-tag'=>checkTags($bbp_topic_tags)); 
       }
       return $topic_data;
    }
    add_filter( 'bbp_new_topic_pre_insert', 'restrict_new_topic_tags' );
    
    //change return the correct tag on a form error submission, we might need to go through the spam check here. But lets see how this goes
    function loadPostTagArray( $topic_tags ) {
       if( $topic_tags == 'Array'){
            if ( bbp_is_post_request() && isset( $_POST['bbp_topic_tags'] ) ) {
                $bbp_topic_tags = filter_input(INPUT_POST, 'bbp_topic_tags', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
                $topic_tags = returnFormattedTags($bbp_topic_tags); 
            }
       } 
       return $topic_tags;
    }
    add_filter( 'bbp_get_form_topic_tags', 'loadPostTagArray' );
    
    function returnFormattedTags($topicTags){
        foreach ($topicTags as $key => $topicTag){
          if($key > 0) $terms .= ', ';
          $terms .= esc_attr( strip_tags( $topicTag ) );
        }
       return $terms;
    }
    
    function checkTags($selectedTags){
        $tags = get_categories( array('hide_empty'=> 0,'taxonomy' => 'topic-tag'));
        $toReturn = array();
        foreach($tags as $tag){
            if (in_array($tag->name, $selectedTags)) {
               $toReturn[] = $tag->name;
            }
        }
        return $toReturn;
    }
    
    function displayTagElements($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[] = '<select class="chzn-select" multiple="multiple" name="bbp_topic_tags[]" id="bbp_topic_tags">';
                foreach($tags as $tag){
                    $selected = '';
                    if (in_array($tag->name, $selectedTagsArray)) {
                       $selected = 'selected="selected"';
                    }
                    $html[] = '<option '.$selected.' value="'.$tag->name.'">'.$tag->name.'</option>';
                }
            $html[] = '</select>';
        }else{
            $html[] = $selectedTags;
        }
        return implode('',$html);
    }

    23creative
    Participant

    @23creative

    Hey Guys,

    i have just completed this functionlaity for my site. i wanted to use a jquery chosen as opposed to a text input so in my themes functions.php file i added the following:

    function restrict_topic_tags( $terms, $topic_id, $reply_id ) {
       $terms = bbp_get_topic_tag_names( $topic_id );
       foreach ($_POST['bbp_topic_tags'] as $topicTags){
          $terms .= ', '.esc_attr( strip_tags( $topicTags ) );
       }
       return $terms;
    }
    add_filter( 'bbp_new_reply_pre_set_terms', 'restrict_topic_tags' );
    
    function displayTagElements($selectedTags){
        $html = array();
        $tags = get_categories( array('hide_empty'=> 0,'taxonomy' => 'topic-tag'));
        $selectedTags = explode(', ', $selectedTags);
        //die(var_dump($selectedTags));
        $html[] = '<select class="chzn-select" multiple="multiple" name="bbp_topic_tags[]" id="bbp_topic_tags">';
            foreach($tags as $tag){
                $selected = '';
                if (in_array($tag->name, $selectedTags)) {
                   $selected = 'selected="selected"';
                }
                $html[] = '<option '.$selected.' value="'.$tag->slug.'">'.$tag->name.'</option>';
            }
        $html[] = '</select>';
        return implode('',$html);
    }

    then in my bbpress custom theme files (form-reply.php, any others that had the reply form. I’m sure there is more than one i haven’t fully checked yet) i added the following code at line 75

    <p>
        <label for="bbp_topic_tags"><?php _e( 'Tags:', 'bbpress' ); ?></label><br />
        <?php echo displayTagElements(bbp_get_form_topic_tags()); ?>
    </p>
Viewing 4 replies - 1 through 4 (of 4 total)