Coding

String Replacement; or, how to not hack the core.

A common requirement for a BuddyPress site is to change some of the default text around the site. If the text originates in a theme, it is straightforward to create a child theme and make the change directly into the template.

However, if the text that you want to change is within a core php file, or another plugin, the correct approach is to create a language file. Usually used for translations, these language files can also be used to load any text customisation. For full information on both of these, please see the “Customizing Labels, Messages, and URLs” page on the BuddyPress codex.

For plugin developers, sometimes situations come up where you need to change a text string in a theme or plugin and, for whatever reason, you can’t create a child theme, edit a plugin’s core files, or load a custom translation. To solve this problem, I wrote the function below which will insert or update a text string inside the translation table.

This implementation was based on a similar, but different, approach by WordPress core developer Peter Westwood on his blog.

* Replaces a string in the internationalisation table with a custom value.

*

* @global object $l10n List of domain translated string (gettext_reader) objects

* @param string $find Text to find in the table

* @param string $replace Replacement text

*/

function dpa_bpt_override_i18n( $find, $replace ) {

global $l10n;

if ( isset( $l10n['buddypress'] ) && isset( $l10n['buddypress']->entries[$find] ) ) {

$l10n['buddypress']->entries[$find]->translations[0] = $replace;

} else {

$mo = new MO();

$mo->add_entry( array( 'singular' => $find, 'translations' => array( $replace ) ) );

if ( isset( $l10n['buddypress'] ) )

$mo->merge_with( $l10n['buddypress'] );

$l10n['buddypress'] = $mo;

}

About Bowe Frankema

 String Replacement; or, how to not hack the core. Building communities on BuddyPress is something I’m very passionate about. The decision to focus exclusively on BuddyPress projects over the last few years inspired me to share back some of the things I've learned here on BP-Tricks! You can find me on WordCamps & BuddyCamps across the globe and on Twitter! Follow me on Twitter

I'm constantly updating my BuddyPress Hosting Guide and SiteGround and WPEngine
have proven to be a great fit for hosting your BuddyPress community.

Need some help finding the perfect host? Check out my in-depth BuddyPress Hosting guide!

Discussion

One response to “String Replacement; or, how to not hack the core.

  1. I still have the issue of my slug for the group forum not changing.

    I have managed to change its display but not its slug.

    The define( ‘BP_FORUMS_SLUG’, ‘forums’ ); is for the main forums group. I want to change the product forum slug.

    Any ideas?

    Johnny

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>