overwrite functions with theme's functions.php
-
How can i overwrite bbpress functions? I want to pass custom arguments in:
function bbp_get_topic_admin_links()
function bbp_get_reply_admin_links()
and
function bbp_list_forums();example:
i want to remove the seperator form bbp_list_forums: ‘separator’ => ‘, ‘
now i modified the corresponding template.php files. but i think its better to find the righ hooks to pass custom arguments to those functions?!thanks!
-
Functions in bbpress are held in the template-tag files
so for instance
bbp_reply_admin_links is held in bbpress/includes/replies/template-tags.phpbbp_topic_admin_links is held in bbpress/includes/topics/template-tags.php
bbp_list_forums is held in bbpress/includes/forums/template-tags.php
open these up and you’ll see what argumens they use, and how to filter – from your post I am presuming you know how to create filters.
Yes. So this is my code which works:
add_filter('bbp_list_forums','custom_bbp_list_forums',10); function custom_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' => '', 'link_after' => '', 'count_before' => '<span>', 'count_after' => '</span>', 'count_sep' => '</span><span>', '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']; } } // Output the list echo apply_filters( 'bbp_list_forumss', $r['before'] . $output . $r['after'], $r ); } }
but maybe there is an easier way to modify arguments array?!
Most bbp functions have simple variables. Yes, there must be a shorter way, but this is an array not a simple variable, and it builds an unordered list using the array and a loop.
It has an “apply filters” as part of the function, so they are expecting it to be able to be filtered, but hopefully not by having to do it all again !
I am writing some codex guide for filtering in bbPress, and by co0incidence was just using this oen as an example, and have been trying to crack it.
I can do it by amending the template –
https://codex.bbpress.org/layout-and-functionality-examples-you-can-use/ section 2, and save template into
wp-content/themes/yourthemename/bbpress
bbPress then uses that one.
I have posted a fresh query to Stephen Edgar or JJJ to pick up, but it’s gone into moderation as I posted a whole lot of code. Hopefully one of the two will pick it up and answer.
In the meantime yours is so far the best answer !
Ok, I’ve had a think, and another look.
There is no easy way to do less than you’ve done.
The problem is that by the time the “apply filters” has been applied, the $output variable has already been created using the input variables, so changing $r is too late.
What this function needs is an earlier apply filters called say ‘bbp_list_forums_args’ for the $r variable, the you could add a simple filter along the lines of
function hide_forum_counts ($r) { $r['show_topic_count'] = false ; $r['show_reply_count'] = false ; $r['separator'] = ' '; return $r ; } add_filter('bbp_list_forums_args','hide_forum_counts') ;
I’ve have suggested this is a trac ticket
yea. such a solution would be awesome and keeps updates of bbpress more save!
because the separator value was annoying to me, i had to create such a big function just to overwrite this simple argument.I’ll let you know if it gets changed.
Were the other two functions as bad?
@nicmare To remove the separator:
add_filter('bbp_before_list_forums_parse_args', 'nicmare_bbpress_list_forums' ); function nicmare_bbpress_list_forums() { $args['separator'] = ''; return $args; }
A good explanation for
bbp_get_topic_admin_links
andbbp_get_reply_admin_links
is here:@netweb Stephen, Thanks for this, and having now looked at the trac ticket and bbp_parse_args function I see how this works.
Wherever you see bbp_parse_args, there’s a filter for the function to do this.
bbp_parse_args has the format
function bbp_parse_args( $args, $defaults = array(), $filter_key = '' )
The resultant filter name is made up of bbp_before_’ . $filter_key . ‘_parse_args’
(there is an bbp_after one as well!)
The filter key is the third argument in the bbp_parse_args
$r = bbp_parse_args( $args, array( 'before' => '<ul class="bbp-forums-list">', 'after' => '</ul>', 'link_before' => '', 'link_after' => '', 'count_before' => '<span>', 'count_after' => '</span>', 'count_sep' => '</span><span>', 'separator' => '', 'forum_id' => '', 'show_topic_count' => true, 'show_reply_count' => true, ), 'list_forums' );
so ‘list_fourms’ in the last line is the third argument.
This should help you for the other two functions, as filters will then be obvious !
yea. finally! thank you guys for making that clear.
- You must be logged in to reply to this topic.