Skip to content

Instantly share code, notes, and snippets.

@pbrocks
Last active October 5, 2021 01:30
Show Gist options
  • Save pbrocks/650a62f0f12e6e2e3c9967f6e36fa283 to your computer and use it in GitHub Desktop.
Save pbrocks/650a62f0f12e6e2e3c9967f6e36fa283 to your computer and use it in GitHub Desktop.
Sometimes we may want to change the wording of the language used in a plugin or theme, but don't want to edit code directly. A cleaner way to go is to use the built in filter that WordPress has called 'gettext'. This filter will search your codebase for translatable strings and replace when an exact match is found.
<?php
/**
* This filter will search your codebase for translatable strings and replace when an exact match is found.
*
* Here we're changing 'Membership' to 'Subscription' for Paid Memberships Pro.
*
* Add this code to your PMPro Customizations Plugin
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
* Note: When adding to your Customizations Plugin, be careful not to include the opening php tag on line 1 above.
*
* @param string $output_text this represents the end result
* @param string $input_text what is written in the code that we want to change
* @param string $domain text-domain of the plugin/theme that contains the code
*
* @return string the result of the text transformation
*/
function my_gettext_membership( $output_text, $input_text, $domain ) {
if ( ! is_admin() && 'paid-memberships-pro' === $domain ) {
$output_text = str_replace( 'Membership Level', 'Subscription', $output_text );
$output_text = str_replace( 'membership level', 'subscription', $output_text );
$output_text = str_replace( 'membership', 'subscription', $output_text );
$output_text = str_replace( 'Membership', 'Subscription', $output_text );
}
return $output_text;
}
add_filter( 'gettext', 'my_gettext_membership', 10, 3 );
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Change the wording ‘Membership’ to ‘Subscription’ for Paid Memberships Pro" at Paid Memberships Pro here: https://www.paidmembershipspro.com/using-str_replace-change-plugin-generated-language/

@ideadude
Copy link

ideadude commented Oct 5, 2021

Newer version that supports _n calls here: https://gist.github.com/ideadude/ab26eb858920b7bcb546d658e6c92178

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment