Get ready for an amazing snippet of code that will allow you to Change any WordPress Text on your entire site. This is WordPress Ninja stuff here and oh so useful!!!
Is there an output of text perhaps in a plugin that you want to change but you do not want to mess with the actual plugin code? Even if you did want to adjust the files inside a plugin to change the text this would be overwritten when there are future updates to the plugin.
Let us give you a real world use for this. A massive part of our site is ticket credit that customers purchase to redeem at a later date for WordPress Support. The plugin we use to distribute this ticket credit is WooCommmerce Smart Coupons. Now the way this plugin is built is that it outputs the text Store Credit and Coupons through the areas it displays info. We wanted to change this output to Ticket Credit. So instead of editing over 13 PHP plugin files and over 1000 instances of these words, we simply used the snippet shared below to do this. The best part is we can update the plugin as new versions are released and we never have to worry about our custom text changing. Change any WordPress Text can have many uses.
Below is the snippet you need to add to the functions.php file of your active theme.
Be sure to change the 2 items “ORIGINAL TEXT” and “NEW TEXT” to your desire.
/* Change Text Site Wide */ function wpfi_change_text( $translated_text ) { if ( $translated_text == 'ORIGINAL TEXT' ) { $translated_text = 'NEW TEXT'; } return $translated_text; } add_filter( 'gettext', 'wpfi_change_text', 20 );
The “NEW TEXT” will also accept HTML so you can get really creative with this.
June 3, 2018 at 10:39 pm
can you do this more than once because when I try to add it again in the functions it makes the whole wordpress page blank, works fine if done once? thoughts?
June 4, 2018 at 9:47 am
You must change the function name if you do it twice. A WP function can not have the same name as another WP function.
August 2, 2018 at 11:36 pm
Yes you can, if you create an IF for every new text.
Like this:
add_filter( ‘gettext’, ‘wpfi_change_text’, 20 );
function wpfi_change_text( $translated_text ) {
if ( $translated_text == ‘ORIGINAL TEXT1’ ) {
$translated_text = ‘NEW TEXT1’;
}
if ( $translated_text == ‘ORIGINAL TEXT2’ ) {
$translated_text = ‘NEW TEXT2’;
}
if ( $translated_text == ‘ORIGINAL TEXT3’ ) {
$translated_text = ‘NEW TEXT3’;
}
if ( $translated_text == ‘ORIGINAL TEXT4’ ) {
$translated_text = ‘NEW TEXT4’;
}
return $translated_text;
}
August 2, 2018 at 10:19 pm
Great snippet!
However, it does not work everywhere.
I’m trying to fix an issue with woocommerce checkout field errors, and this snippet doesen’t affect these texts.
Is there any way to use a snippet to change field error messages as well?
August 8, 2018 at 8:43 am
Would need to edit the actual WooCommerce template files.
November 21, 2018 at 5:15 am
Thanks for this snippet. It saved me lots of time.
January 23, 2019 at 2:36 pm
Thank you! It helped me a lot!
Tamas
January 24, 2019 at 8:59 am
Our pleasure!
February 25, 2019 at 8:40 am
Nice little snippet, thank you, I’ve used it on a theme that kept changing the copyright text to the default text each time I saved the options.
How about making this work on Dashboard only? Will this be possible?
February 26, 2019 at 8:40 am
Sure can with an IF ELSE PHP statement.
April 16, 2019 at 3:48 pm
Perfect, thank you!
April 18, 2019 at 9:06 am
Our pleasure!
April 23, 2019 at 11:16 am
this would be better as a plugin so that if the template or site gets updated it doesnt erase the code snippet
April 23, 2019 at 11:18 am
There are already many plugins that do this. This is another way that is simple and not plugin or database entries needed.
September 9, 2019 at 10:13 am
Our pleasure!