How to Translate Any Text String in WordPress

Traditional translation techniques tend to be highly technical and require the use of multiple files to reach the desired outcome.

But you can skip all this nonsense by using the following snippet of code to translate anything in WordPress from the comfort of your preferred custom PHP file:

add_filter('gettext', 'translate_anything');

function translate_anything($text) {
	if ($text === '[before]') {
		$text = '[after]';
	}
	return $text;
}

In the above code, replace [before] with the original text you want to translate, and then replace [after] with your translated text.

You can repeat this simple if-statement for as many strings as you wish to translate!