0

Frequently I need to change specific bits of text within Wordpress plugins, usually buttons and usually set within the plugin files as strings.

Sometimes an overide within my child theme does the trick but in most cases seems like overkill to change one or two words. I've had some success using the 'gettext' hook, but it has been suggested this is bad practice as I'm 'pushing content through the eye of a needle'.

In the example below I'm able to change the text for two buttons specifically within the Memberpress plugin. It seems to work okay but is there a better way of doing this?

function mepr_account_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Update' :
            $translated_text = __( 'Update Card', 'memberpress' );
            break;
        case 'Cancel' :
            $translated_text = __( 'Cancel Subscription', 'memberpress' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'mepr_account_strings', 20, 3 );
Reason65
  • 1
  • 1
  • A better way could be to copy and change the translation file. This answer might help: https://stackoverflow.com/a/41225515/1678383 To edit translation files, you need a program like PoEdit. – Levi Jan 30 '23 at 14:52
  • Memberpress is a closed-source and expensive plugin. They brag about the ability to customize their checkout pages. Have you asked their support team for help? – O. Jones Jan 30 '23 at 14:54
  • @Levi, That looks promising, thanks! – Reason65 Jan 30 '23 at 16:53
  • @O.Jones I'm sure Memberpress would help out in this case, but I'm hoping there is a general way to edit these strings that would work for any plugin. Thanks. – Reason65 Jan 30 '23 at 16:58
  • You're right that the gettext filter is an inefficent way to substitute text. A better way is to use a more specific filter. A good way to find a more specific filter is to look at the source code for the plugin and see what it does. But Memberpress isn't open source. You, or somebody with access to Memberpress, could use a debugger and set a breakpoint, and step through the code that delivers the form to find a good filter. But that's hard work. – O. Jones Jan 30 '23 at 18:53

0 Answers0