1

I am new to WP Plugin development. I have a theme where texts are included inside __() function for translations, like:

__('This is text to be translated');

I want to create a plugin which would get the value from __() functions and run through my own defined functions. Suppose the text inside __() contains

__("This is JavaScript alert <script>alert('Hello');</script>");

I want my plugin to do sanity check before printing it in the browser, something like:

function my_sanitizer($text) {
  $text = trim(strip_tags($text)); // The $text is value from __() of above
  return $text;
}

and the sanitized text should print in the browser.

How shall I be approaching this, any hints, ideas, solutions would be appreciated.

2

You should look at gettext filter hook which is applied to the translated text by the internationalization functions (__(), _e(), etc.). eg:

add_filter( 'gettext', 'my_sanitizer', 20, 3 );
/**
 * my_sanitizer
 * @param  string $translated_text the translation
 * @param  string $text            the origial text from __()
 * @param  string $domain          text domain
 * @return string
 */
function my_sanitizer( $translated_text, $text, $domain ) {

    $translated_text = trim(strip_tags($translated_text)); 

    return $translated_text;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy