Get $100 Digitalocean credit
When we develop WordPress plugin/theme, we need to use translatable function(gettextFunctions) and also escaping text. In WordPress have some functions for scaping and translating text. like (), esc_html(), _e(), esc_html_e()
We declare those functions like.
__('Hello Awesome WordPress', 'my-plugin-text-domain');
But problem is that we declare those functions many places with our text domain it's repeating same text again and again and if we need to change our text domain then need to change all files where declared those functions.
Let's try to solve the problem.
- Create a file in your plugin/theme for global function declaring. file name like:
global-functions.php
- declaring following functions:
1
if ( !function_exists( 'mp_text' ) ) {
function mp_text( $text, $fn = '__' ) {
return $fn( $text, 'my-plugin-text-domain' );
}
}
*** This function will return translated text with __()
function but you can pass any function related to same purpose.
*** mp
is my plugin short name. Add sort prefix of your plugin/theme name.
*** you can declare the function with your desired name but must use a sort prefix.
2
if ( !function_exists( 'mp_text_p' ) ) {
function mp_text_p( $text, $fn = '_e' ) {
$fn( $text, 'my-plugin-text-domain' );
}
}
*** This function will print translated text with _e()
function but you can pass any function related to same purpose.
*** mp
is my plugin short name. Add sort prefix of your plugin/theme name.
3
if ( !function_exists( 'mp_title' ) ) {
function sep_title( $text, $fn = 'esc_html__' ) {
return $fn( $text, 'my-plugin-text-domain' );
}
}
*** This function will return translated text with html escaping with esc_html__()
function but you can pass any function related to same purpose.
*** You can declare the function with your desired name but must use a sort prefix.
4
if ( !function_exists( 'mp_title_p' ) ) {
function sep_title_p( $text, $fn = 'esc_html__' ) {
echo $fn( $text, 'my-plugin-text-domain' );
}
}
*** This function will print translated text after html escaping with esc_html__()
function but you can pass any function related to same purpose.
*** If you have question that when we create pot file through any tool, we must use those function not custom wrapper functions.
Answer is no, every pot file generating tool's have custom functions adding system for filtering php file. You can check
https://github.com/wp-pot/wp-pot
Uses
mp_title_p('Hello Awesome WordPress');
echo mp_text('Hello Awesome WordPress');
echo mp_text('container', 'esc_attr__');
Top comments (0)