How to change blog button text?

Such as “LEAVE A COMMENT” “SUBMIT”

Hi @junbug1,

  • You have to setup a child theme first:
  • After that add this custom code under functions.php file locates in your child theme:
add_filter( 'gettext', 'translate_x_strings', 20, 3 );

function translate_x_strings( $translated_text, $text, $domain ) {

        switch ( $translated_text ) {

            case 'Leave a Comment' :

                $translated_text = __( 'Your new text', '__x__' );
                break;

            case "Submit" :

                $translated_text = __( 'Your new text', '__x__' );
                break;

        }

    return $translated_text;
}

Hope it helps :slight_smile:

Thanks!

How to replace that with a different language?
For example, I want to change that into Chinese.

Wordpress doesn’t seem to load the string after I did that.

Jun

Hi Jun,

You need to change the text Your new text with the translated chinese version.

Other option is to translate using po edit or use wpml plugin.

For a detailed guide kindly refer to the link below

Thanks

I did replace the “Your new text” with the Chinese.
WordPress website will not function after I changed that.

syntax error, unexpected ’ ’ (T_STRING)

I know wpml but I don’t need to translate the link on my website.
When you pick a different language in WordPress admin panel, WordPress default language will change(include WooCommerce interface etc…)For X theme, there is only links’ language did not get changed.

Hi Jun,

Those are parse errors would cause the site to crash completely and be inaccessible. It happens when you add PHP code with invalid syntax. Whenever editing a child theme, it’s best to use something like FTP so if it goes down you can still edit the code of your file without needing to restore a site backup.

Something that might help is copying the entire contents of your file into a syntax checker like this one: https://phpcodechecker.com/

That might give a better clue into what’s wrong.

Thanks!

Can you check my previous comments?

It’s working fine if I replace the “Your new text” with English.
It doesn’t work If I replace that with Chinese or other language maybe…

Jun

Hi Jun,

Would you mind providing the final code you have added? That’s sounds an internal error but replacing the string shouldn’t do that. I have to check the complete code that you added after the string replacement.

Thanks!

add_filter( 'gettext', 'translate_x_strings', 20, 3 );

function translate_x_strings( $translated_text, $text, $domain ) {

        switch ( $translated_text ) {

            case 'Leave a Comment' :

                $translated_text = __( '留言', '__x__' );
                break;

            case "Submit" :

                $translated_text = __( 'English will work', '__x__' );
                break;

        }

    return $translated_text;
}

Hello Jun,

Please double check the single quotes especially if you are copying and pasting the text. Most probably, you have inserted the wrong quotes. PHP will only accept this 'some text' and "some text" quotes.

Hope this helps.

Can you help me check this code??
Or test it on your test server?

Jun

Hello Jun,

You might be referring to the wrong keyword. You can update the code into this:

add_filter( 'gettext', 'translate_x_strings', 20, 3 );

function translate_x_strings( $translated_text, $text, $domain ) {

        switch ( $translated_text ) {

            case "Leave a Comment" :

                $translated_text = __( '留言', '__x__' );
                break;

            
            case "%s Comment" :

                $translated_text = __( '留言 %s', '__x__' );
                break;

            case "%s Comments" :

                $translated_text = __( '留言 %s', '__x__' );
                break;

            case "Leave a comment on: “%s”" :

                $translated_text = __( '留言 : “%s”', '__x__' );
                break;

            case "Submit" :

                $translated_text = __( 'English will work', '__x__' );
                break;

        }

    return $translated_text;
}

We would love to know if this has worked for you. Thank you.

Your code works.

I am kind of confused. If I change “留言” to some other things, then the code doesn’t work again.

add_filter( 'gettext', 'translate_x_strings', 20, 3 );

function translate_x_strings( $translated_text, $text, $domain ) {

        switch ( $translated_text ) {

            case "Leave a Comment" :

                $translated_text = __( '发表评论', '__x__' );
                break;

            
            case "%s Comment" :

                $translated_text = __( '评论 %s', '__x__' );
                break;

            case "%s Comments" :

                $translated_text = __( '评论 %s', '__x__' );
                break;

            case "Leave a comment on: “%s”" :

                $translated_text = __( '发表评论 : “%s”', '__x__' );
                break;

            case "Submit" :

                $translated_text = __( '提交', '__x__' );
                break;

        }

    return $translated_text;
}

Hi Jun,

Seems to be working on my end, would you mind providing the following information in the secure note?

  • site’s URL
  • Admin and FTP login credentials

I like to check which isn’t working, the translation or internal error.

Thanks!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.

Hello Jun,

After changing the translation keyword, you must clear your plugin cache since you have installed SG Optimizer plugin. And by the way, I have fixed your language files. Instead of using zh_CN.xx, I have rename it to the correct and proper filename which is __x__-zh_CN.xx. You can add your translation so that it will work now.

Best Regards.