Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to filter an array


  • Robin W
    Moderator

    @robin-w

    ok, so I know how to filter variables (and am documenting for the codex), but I cannto figure the syntax for an array.

    As an example (and apologies for long code – but in this case hope you’ll let me off!)

    function bbp_list_forums( $args = '' ) {
    
    	// Define used variables
    	$output = $sub_forums = $topic_count = $reply_count = $counts = '';
    	$i = 0;
    	$count = array();
    
    	// Parse arguments against default values
    	$r = bbp_parse_args( $args, array(
    		'before'            => '<ul class="bbp-forums-list">',
    		'after'             => '</ul>',
    		'link_before'       => '<li class="bbp-forum">',
    		'link_after'        => '</li>',
    		'count_before'      => ' (',
    		'count_after'       => ')',
    		'count_sep'         => ', ',
    		'separator'         => ', ',
    		'forum_id'          => '',
    		'show_topic_count'  => true,
    		'show_reply_count'  => true,
    	), 'list_forums' );
    
    	// Loop through forums and create a list
    	$sub_forums = bbp_forum_get_subforums( $r['forum_id'] );
    	if ( !empty( $sub_forums ) ) {
    
    		// Total count (for separator)
    		$total_subs = count( $sub_forums );
    		foreach ( $sub_forums as $sub_forum ) {
    			$i++; // Separator count
    
    			// Get forum details
    			$count     = array();
    			$show_sep  = $total_subs > $i ? $r['separator'] : '';
    			$permalink = bbp_get_forum_permalink( $sub_forum->ID );
    			$title     = bbp_get_forum_title( $sub_forum->ID );
    
    			// Show topic count
    			if ( !empty( $r['show_topic_count'] ) && !bbp_is_forum_category( $sub_forum->ID ) ) {
    				$count['topic'] = bbp_get_forum_topic_count( $sub_forum->ID );
    			}
    
    			// Show reply count
    			if ( !empty( $r['show_reply_count'] ) && !bbp_is_forum_category( $sub_forum->ID ) ) {
    				$count['reply'] = bbp_get_forum_reply_count( $sub_forum->ID );
    			}
    
    			// Counts to show
    			if ( !empty( $count ) ) {
    				$counts = $r['count_before'] . implode( $r['count_sep'], $count ) . $r['count_after'];
    			}
    
    			// Build this sub forums link
    			$output .= $r['link_before'] . '<a href="' . esc_url( $permalink ) . '" class="bbp-forum-link">' . $title . $counts . '</a>' . $show_sep . $r['link_after'];
    		}
    
    		// Output the list
    		echo apply_filters( 'bbp_list_forums', $r['before'] . $output . $r['after'], $r );
    	}
    }

    If I wanted to turn off the topic and reply counts, I could use a filter which would look something like

    function hide_forum_counts () {
    $r['show_topic_count'] = false ;
    $r['show_reply_count'] = false ;
    $r['separator']  = ' ';
    return $r ;
    }
    add_filter('bbp_list_forums','hide_forum_counts') ;

    Stephen, JJJ or other bbp genius – can you post the answer, I have tried everything to prevent needing to copy all the original function, but have failed

Viewing 2 replies - 1 through 2 (of 2 total)
  • This is what you want for the above:

    function hide_forum_counts() {
            $args['show_topic_count'] = false;
            $args['show_reply_count'] = false;
            $args['separator']        = ' '; 
            return $args;
    }
    add_filter('bbp_before_list_forums_parse_args', 'hide_forum_counts' );
    

    A full example of all the bbp_list_forums arguments to use either in a custom function or a plugin https://gist.github.com/ntwb/3797945


    Robin W
    Moderator

    @robin-w

    Thanks Stephen

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