Using gettext for hyperlinks | WordPress.org

[ad_1]

Hi @ronantrelis
To localize the link text in your plugin, you can use WordPress’s __() function. This function retrieves the translation of the string and allows you to specify a text domain for your plugin.

Here’s an example of how you can use it:

<a href="myurl"><?php echo esc_html__('Link Text', 'myplugin'); ?></a>

Use the esc_html__() function to escape the output and prevent security vulnerabilities.

You will also need to create a translation file for your plugin, which contains the translations of the strings used in your plugin. The translation file should be placed in your plugin’s languages folder and have the following naming convention: {text-domain}-{locale}.mo. For example, the translation file for the myplugin text domain in the English (US) locale would be named myplugin-en_US.mo.

You can use a plugin like Poedit to create and manage your translation files.

I hope this helps! Let me know if you have any questions.`

  • This reply was modified 3 hours, 9 minutes ago by Faisal Ahammad. Reason: fixed typo error

Thread Starter
Trelis

(@ronantrelis)

Many thanks @faisalahammad , very helpful.

Here’s what I have now:

'description' => '<a href="https://docs.trelis.com/features/trelis-prime"><?php echo esc_html__('Learn how to minimise payment processing charges', 'trelis-crypto-payments'); ?></a>',

and here’s the error I’m getting:

Parse error: syntax error, unexpected ‘Learn’ (T_STRING), expecting ‘)’ in /home/customer/www/mysite.com/public_html/wp-content/plugins/Trelis-ethereum-payment-gateway.php_/Trelis-ethereum-payment-gateway.php on line 190

Hi @ronantrelis
It looks like there is an issue with the PHP code in your plugin file. The problem is that you are trying to use PHP tags and functions inside a single-quoted string, which is not allowed.

To fix the issue, you will need to either use double quotes to define the string or concatenate the string and the PHP code using the . operator:

Option 1:
'description' => "<a href='https://docs.trelis.com/features/trelis-prime'>" . esc_html__('Learn how to minimise payment processing charges', 'trelis-crypto-payments') . "</a>",

Option 2:
'description' => '<a href="https://docs.trelis.com/features/trelis-prime">' . esc_html__('Learn how to minimise payment processing charges', 'trelis-crypto-payments') . '</a>',

Either of these options should resolve the parse error you are seeing.

I hope this helps! Let me know if you have any other questions.

Thread Starter
Trelis

(@ronantrelis)

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer