Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filtering Title in BuddyPress


  • BobSD99
    Participant

    @bobsd99

    I’m working on filtering titles for BuddyPress pages (specifically, Groups and Group Forums). I’m having some trouble so maybe someone can ppint me in the right direction

    Since 4.1, WordPress has deprecated the wp_title() function, and instead developers should use the document_title_parts filter. The filter is documented in the Code Reference, and an example using this filter can be seen here.

    I’ve successfully used this filter on my very basic Twentysixteen child theme, but that method doesn’t seem to adopt to a Group page template I created and the forums that I’ve created and assigned under them. Here was a basic hack I used for testing purposes:

    // output a custom title for Groups & Forums
    function custom_groups_title( $title ){
    
        if ( bp_is_group() ){
    
            $group_name = bp_get_current_group_name();
    
            if ( bp_current_action() == 'forum') {
                $group_name .= " Chatboard";
            }
    
            $title['title']     = $group_name . ' | Awesome Website, Inc.';               
            $title['site']      = '';
            $title['tagline']   = '';
        }    
    
        return $title;
    }
    add_filter('document_title_parts', 'custom_groups_title', 10);
    

    the title is not getting filtered at all, so I guess BuddyPress is bypassing or overriding this filter.

    Can anyone point me in the right direction for filtering the title?

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

  • Henry Wright
    Moderator

    @henrywright

    so I guess BuddyPress is bypassing or overriding this filter

    If that were the case, you can increase the priority:

    add_filter('document_title_parts', 'custom_groups_title', 20);

    But I don’t think that’s the problem in this case. Maybe check if bp_is_group() and bp_current_action() are working as you expect them to be. var_dump( $some_value ); will be useful here.


    Henry Wright
    Moderator

    @henrywright

    Also, $title should be a string, not an array. So doing stuff like this $title['title'] won’t work.


    BobSD99
    Participant

    @bobsd99

    Hi Henry – thanks for response. As confirmed by both the Codex (Code Reference) and my successful use of this method in the same installation outside of BuddyPress, $title is indeed an array.

    In fact, I tested my function without the if ( bp_is_group() ){ conditional, and the filter works on the pages NOT created/affected by BuddyPress, which confirms BuddyPress is bypassing this filter.

    For testing purposes, I modified the function as follows:

    // test parameters passed to document_title_parts filter
    function test_groups_title( $title ){
    
    	print_r( $title );
    	exit;
    
    }
    add_filter('document_title_parts', 'test_groups_title', 10);

    And BuddyPress pages output the following array:

    Array
    (
        [title] => 
        [site] => My-Website.com
    )

    Which suggests that BuddyPress is neither supplying data to this array nor utilizing it for the title output.

    I did a grep of BuddyPress and bbPress and I am finding calls to the deprecated wp_title() function:

    $ grep -r 'wp_title' ../plugins/b*press/*
    ../plugins/bbpress/includes/core/filters.php:add_filter( 'wp_title',                'bbp_title',              10, 3 );
    ../plugins/bbpress/readme.txt:* Fix custom wp_title compatibility
    ../plugins/buddypress/bp-core/bp-core-filters.php: * @see wp_title()
    ../plugins/buddypress/bp-core/bp-core-filters.php:       * @see wp_title()
    ../plugins/buddypress/bp-core/bp-core-filters.php:       * Filters the older 'wp_title' page title for BuddyPress pages.
    ../plugins/buddypress/bp-core/bp-core-filters.php:add_filter( 'wp_title',             'bp_modify_page_title', 20, 3 );
    ../plugins/buddypress/bp-core/deprecated/1.5.php: * @deprecated Use wp_title()
    ../plugins/buddypress/bp-core/deprecated/1.5.php:        * Now, just simply use wp_title().
    ../plugins/buddypress/bp-core/deprecated/1.5.php:        * @deprecated Use wp_title()
    ../plugins/buddypress/bp-core/deprecated/1.5.php:               _deprecated_function( __FUNCTION__, '1.5', 'wp_title()' );
    ../plugins/buddypress/bp-core/deprecated/1.5.php:               $title = wp_title( '|', false, 'right' ) . get_bloginfo( 'name', 'display' );
    ../plugins/buddypress/bp-themes/bp-default/header.php:          <title><?php wp_title( '|', true, 'right' ); bloginfo( 'name' ); ?></title>
    ../plugins/buddypress/bp-themes/bp-default/archive.php:                 <h3 class="pagetitle"><?php printf( __( 'You are browsing the archive for %1$s.', 'buddypress' ), wp_title( false, false ) ); ?></h3>

    I guess this means I can and should hook in to the older (deprecated) wp_title() function? I’m a bit new to BuddyPress and WordPress, so maybe I’m missing something obvious, or barking up the wrong tree here….


    Henry Wright
    Moderator

    @henrywright

    For some reason I was looking at document_title_separator which is why I thought your function was passed a string. You are right, $title is an array. Sorry about that 🙂


    BobSD99
    Participant

    @bobsd99

    I didn’t have any luck with the wp_title() function/hook either, so I’ll next climb into the core a bit to see if I can get anywhere. Reference and implementations for title functions in BuddyPress seem to be scarcely documented on the web, so I was hoping to get some insight from the experts here, as well as leave a few breadcrumbs for developers in the future. That could be especially helpful as WP/BP transition away from the wp_title() function.


    Henry Wright
    Moderator

    @henrywright

    I did a bit of digging for you. The filter you need to use is bp_title_parts. Your hooked function will be passed an array of the BuddyPress title parts.

    Ref: bp-core/bp-core-template.php#L3101


    BobSD99
    Participant

    @bobsd99

    Bingo! Thanks Henry… 🙂

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