Created
July 28, 2025 20:19
-
-
Save sbrajesh/f78ecdea1408b44b98b2b788c7cbbbb8 to your computer and use it in GitHub Desktop.
BuddyBoss extended topics index shortcode to allow filtering by topic creator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Extends topic shortcide for BuddyBoss to allow filtering by author. | |
| */ | |
| class BBP_Extended_Shortcode { | |
| /** | |
| * Cached attributes for the current shortcode instance. | |
| * | |
| * @var array Attributes | |
| */ | |
| private array $atts = array(); | |
| /** | |
| * Sets up shortcode. | |
| */ | |
| public function setup() { | |
| add_shortcode( 'bbp-topic-index-extended', array( $this, 'shortcode' ) ); | |
| } | |
| /** | |
| * Extended bbPress topic index shortcode. | |
| * | |
| * @param array $atts Attributes. | |
| * @param string $content Content. | |
| * | |
| * @return string | |
| */ | |
| public function shortcode( $atts = array(), $content = null ) { | |
| $atts = shortcode_atts( array( | |
| 'author' => bp_displayed_user_id(), | |
| ), $atts ); | |
| $this->atts = $atts; | |
| if ( ! function_exists( 'bbpress' ) ) { | |
| return ''; | |
| } | |
| $bbp_shortcodes = bbpress()->shortcodes; | |
| // Bail if shortcodes are unset somehow | |
| if ( ! is_a( $bbp_shortcodes, 'BBP_Shortcodes' ) ) { | |
| return ''; | |
| } | |
| // hook our author filter. | |
| if ( ! bbp_is_topic_archive() ) { | |
| add_filter( 'bbp_before_has_topics_parse_args', array( $this, 'update_query' ), 200 ); | |
| } | |
| if ( ! is_callable( array( $bbp_shortcodes, 'display_topic_index' ) ) ) { | |
| return ''; | |
| } | |
| return $bbp_shortcodes->display_topic_index(); | |
| } | |
| /** | |
| * Updates bbPress topic query | |
| * | |
| * @param array $args args. | |
| * | |
| * @return array | |
| */ | |
| public function update_query( $args = array() ) { | |
| if ( ! empty( $this->atts['author'] ) ) { | |
| $args['author'] = $this->atts['author']; | |
| unset( $this->atts['author'] ); | |
| } | |
| return $args; | |
| } | |
| } | |
| ( new BBP_Extended_Shortcode() )->setup(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment