If you use the BuddyPress plugin you may have noticed the <title> tag on BuddyPress subpages is empty or only contains the site name or tagline, not the actual page title. One of our clients uses the BuddyPress and BuddyPress Member Types Pro plugins, and we noticed the BuddyPress user profile and related pages (settings, notifications, etc.) as well as the the custom member type directory pages did not have a set title.
It is important that the <title> tag is set correctly – it is displayed when the page appears in search results and it hurts your SEO score if empty. The <title> tag also defines the title in the browser toolbar.
In order to customize the <title> tag, you need to hook into the appropriate WordPress filter. The filter to be used depends on the version of WordPress and whether or not your theme or any additional plugins already hook into the filter.
If you are using at least WordPress 4.4 or newer, then you can hook into the document_title_parts or pre_get_document_title filter.
In most cases, hooking into document_title_parts should do the trick. The $title parameter is an array containing the title parts: title, page (page number if paging is used), tagline and site. We only need to update the title. You’ll need to change the member type comparison and new title given your site needs (lines 15-19).
//Update title on Buddypress sub pages
function da_bp_set_page_title( $title ){
global $bp;
$new_title = "";
//If we're on a user's profile page (or settings, notifications, etc.)
//then set the new title as that user's fullname
if( bp_displayed_user_id() ){
$new_title = $bp->displayed_user->fullname;
}
else if ( $bp->current_component == 'members' ) {
//Check the current member type and set appropriate new title
if ( $bp->current_member_type == 'da-mtype-basic') {
$new_title = "Members";
} else if ( $bp->current_member_type == 'da-mtype-elite') {
$new_title = "Elite Members";
}
}
if( strlen($new_title) > 0 ){
$title['title'] = $new_title;
}
return $title;
}
add_filter( 'document_title_parts', 'da_bp_set_page_title');
If the above does not work, your theme or another plugin may be to blame. One common culprit is the Yoast SEO plugin. When pre_get_document_title is used and returns a non-empty value, the originating function wp_get_document_title is completed early and the rest of the function is not evaluated, including document_title_parts. So if another plugin or your theme is already hooking into pre_get_document_title and returning a value, then you must also use pre_get_document_title and with a higher priority.
One thing to keep in mind is that the $title parameter of pre_get_document_title is the *full* title as a single string, meaning it includes the separator and tagline if defined. This is why we combined the new and original title values on line #25 below.
//Update title on Buddypress sub pages
function da_bp_set_title_tag( $title ){
global $bp;
$new_title = "";
//If we're on a user's profile page (or settings, notifications, etc.)
//then set the new title as that user's fullname
if( bp_displayed_user_id() ){
$new_title = $bp->displayed_user->fullname;
}
else if ( $bp->current_component == 'members' ) {
//Check the current member type and set appropriate new title
if ( $bp->current_member_type == 'da-mtype-basic') {
$new_title = "Members";
} else if ( $bp->current_member_type == 'da-mtype-elite') {
$new_title = "Elite Members";
}
}
//Combine the new title with the old (separator and tagline)
if( strlen($new_title) > 0 ){
$title = $new_title . " " . $title;
}
return $title;
}
add_filter( 'pre_get_document_title', 'da_bp_set_title_tag', 999, 1);
If you are not using at least WordPress 4.4, then you can hook into the wp_title filter. The $title parameter in this case is just that – the title as a single string (no separator, tagline, etc.) Check out the hook definition for additional optional parameters if needed.
//Update title on Buddypress sub pages
function da_bp_set_page_title( $title ){
global $bp;
$new_title = "";
//If we're on a user's profile page (or settings, notifications, etc.)
//then set the new title as that user's fullname
if( bp_displayed_user_id() ){
$new_title = $bp->displayed_user->fullname;
}
else if ( $bp->current_component == 'members' ) {
//Check the current member type and set appropriate new title
if ( $bp->current_member_type == 'da-mtype-basic') {
$new_title = "Members";
} else if ( $bp->current_member_type == 'da-mtype-elite') {
$new_title = "Elite Members";
}
}
if( strlen($new_title) > 0 ){
$title = $new_title;
}
return $title;
}
add_filter( 'wp_title', 'da_bp_set_page_title');
Now your BuddyPress subpages should have the proper title!