3

Without using a language file, which file would I go to if I wanted to rename profile tabs? For example, instead of the tab saying "Activity" I want it to say "My Activity". Instead of "Groups" I would make it say "My Groups" etc. Thanks

2 Answers 2

1

Look at the source, you know that already. :)

I didn’t but I bet there is a piece of code looking like this:

_e( 'Activity', 'buddypress' );

… or …

__( 'Activity', 'buddypress' );

Follow the functions, they are wrappers for the function translate() in wp-includes/l10n.php:

/**
 * Retrieves the translation of $text. If there is no translation, or
 * the domain isn't loaded, the original text is returned.
 *
 * @see __() Don't use translate() directly, use __()
 * @since 2.2.0
 * @uses apply_filters() Calls 'gettext' on domain translated text
 *      with the untranslated text as second parameter.
 *
 * @param string $text Text to translate.
 * @param string $domain Domain to retrieve the translated text.
 * @return string Translated text
 */
function translate( $text, $domain = 'default' ) {
    $translations = &get_translations_for_domain( $domain );
    return apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
}

You see you get a filter: 'gettext' with three arguments (or parameters). Now you can use the filter to change the output.
Add this to your theme’s functions.php or to a plugin:

add_filter( 
    'gettext', // filter name
    'wpse_57673_change_buddypress_profile_tabs', // name of your custom function
    10, // priority
    3 // number of arguments you want to get
);

Now we need the custom function, that’s really simple:

function wpse_57673_change_buddypress_profile_tabs( $translated, $original_text, $domain )
{
    if ( 'buddypress' !== $domain )
    {
        return $translated; // not your text
    }

    // find the text to change
    switch ( $original_text )
    {
        case 'Activity':
            return 'My Activity';

        case 'Groups':
            return 'My Groups';

        default:
            return $translated;
    }
}
3
  • "Look at the source, you know that already. :)" (I felt lazy. I looked in the members template file and couldnt find it and I didn't wanna search anymore) Gosh darnit Toscho, you are brilliant :) + What is this? "wpse_57673"
    – Androliyah
    Jul 7, 2012 at 3:04
  • @Aliyah - It is a prefix for making the function name unique, so there's little chance on earth it will conflict with another function declared somewhere (theme or plugin). . . . . . If there are two functions with the same name in your WordPress, the site will break (HTTP Error 500 (Internal Server Error)) due to a Fatal error: Cannot redeclare wpse_57673_change_buddypress_profile_tabs() (previously declared in /path/to/first/function/ocurrence.php) in /path/to/second/function.php. . . . . . It stands for WordPressStackExchange_QuestionIdNumber :)
    – brasofilo
    Jul 7, 2012 at 9:12
  • Ah! I completely understand now. That explains why Ive had past issues with "cannot redeclare"! Thanx a million Brasofilo!
    – Androliyah
    Jul 7, 2012 at 20:32
0

I dug into the code a little bit and found what looks like the "right way" to do it:

function wpse_57673_change_bp_profile_tabs() {

    // Rename a primary nav item.
    buddypress()->members->nav->edit_nav(
        array( 'name' => 'My Activity' )
        , 'activity'
    );

    // Rename a secondary nav item.
    buddypress()->members->nav->edit_nav(
        array( 'name' => 'Saved Posts' )
        , 'favorites'
        , 'activity'
    );
}
add_action( 'bp_setup_nav', 'wpse_57673_change_profile_tabs', 20 );

This is better than hooking into 'gettext', because the 'gettext' filter is called very often and so hooking into it can degrade performance.

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .