Rename Buddypress Profile Tab

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

Related posts

Leave a Reply

2 comments

  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;
        }
    }
    
  2. 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.