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;
}
BuddyPress Hosting Guide Looking for quality BuddyPress hosting? We will guide you through all the available hosting options and help you find the best webhost for your next BuddyPress project!

Something about Paul Gibbs

BuddyPress Core Developer. I am a core committer to BuddyPress, enjoy speaking at WordCamps and consult on projects and design custom plugins for your site; contact me at [email protected] Find me helping out on the BuddyPress.org forums, in #buddypress-dev on freenode, or on twitter as @pgibbs.

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

  1. Johnny July 16, 2011 at 1:45 pm #

    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

    Reply

Leave a Reply