1.1 WordPress Smart Coding Tips | Escaping & translation
Tags: wordpress, wordpressplugin, wordpresstheme
Published: 2021-11-14 04:16:35

---
title: 1.1 WordPress Smart Coding Tips | Escaping & translation
published: true
description: WordPress Smart Coding Tips | Escaping & translation 
tags: wordpress, wordpressplugin, wordpresstheme
//cover_image: https://direct_url_to_image.jpg
---
### [Get $100 Digitalocean credit](https://m.do.co/c/de38dc8716fd)
 
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 <code> __(), esc_html__(), _e(), esc_html_e()</code>

We declare those functions like.
```php
__('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.

1. Create a file in your plugin/theme for global function declaring. file name like:  <code> global-functions.php </code>
2. declaring following functions:

## 1

```php
if ( !function_exists( 'mp_text' ) ) {
	function mp_text( $text, $fn = '__' ) {
		return $fn( $text, 'my-plugin-text-domain' );
	}
}
```
*** This function will return translated text with <code>__()</code> function but you can pass any function related to same purpose.
*** <code>mp</code> 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

```php
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 <code>_e()</code> function but you can pass any function related to same purpose.
*** <code>mp</code> is my plugin short name. Add sort prefix of your plugin/theme name. 

#3

```php
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 <code>esc_html__()</code> 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

```php
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 <code>esc_html__()</code> 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

```php
mp_title_p('Hello Awesome WordPress');
echo mp_text('Hello Awesome WordPress');
echo mp_text('container', 'esc_attr__');
```


[AL EMRAN](https://alemran.me)
[Github](https://github.com/emrancu)


[PluginMaster, a WordPress plugin development framework](https://dev.to/plugin-master)