I know that we're not supposed to write PHP code in the TinyMCE content editor. But here's my predicament: the URLs on the website I'm building include a user language code, e.g. test.com/en/products/product-1. When content is added/edited via the TinyMCE editor, sometimes, hyperlinks need to be added in the text, e.g. "Click here for more details". If the content editor writes <a href="/products/product1">here</a>"
, this URL will not be valid. The link needs to be <a href="/<?php echo $lang; ?>/products/product1">here</a>
. But TinyMCE either deletes this code or makes it unusable.
I've tried adding this to the configuration:
protect: [
/\<\/?(if|endif)\>/g, // Protect <if> & </endif>
/<\?php.*?\?>/g // Protect php code
]
How can I enable PHP input in the editor? Or if there's a different way to achieve the same... I also tried adding a <base href="domain.com/<?php echo $lang; ?>" />
tag but that doesn't work.
It results in this:
href="/<!--mce:protected %3C%3Fphp%20echo%20%24lang%3B%20%3F%3E-->/products/product1"
The easiest and safest way to do this would be to replace the text before the page is sent to the user, rather than attempting to evaluate PHP code. Use placeholders in your text, like this:
<a href="/__LANG__/products/product1">here</a>
Then before the text is sent to the browser, replace the placeholders with the variables you want
$text = strtr($text, ['__LANG__' => $lang]);