add_filter( 'wpml_tm_xliff_unit_placeholders', callable $function_to_add, 10, 3 );
Description
This filter declares the placeholders of a translatable field, for example {first_name} in the text of an email. A placeholder is text that your code replaces with a value, so a translation must return it exactly as it is.
WPML runs the filter for each field of a translation job that it sends to the Advanced Translation Editor. Add one PCRE pattern for each kind of placeholder. WPML marks every match in the text, and the translation comes back with the same placeholder, character for character. Automatic translation never changes it.
Return the patterns that you received together with your own, because other plugins use the same filter. Check $element->field_type, so that your patterns apply only to your own fields.
What the protection covers:
- The Advanced Translation Editor and automatic translation only. A manual XLIFF export and a translation service get the text as it is.
- Text only. A placeholder inside an HTML tag, for example in an attribute value, is not protected.
- The space after a placeholder stays with it. The translator can move the placeholder to another place in the sentence.
For the texts of a page builder, use the wpml_pb_string_placeholder_patterns filter, which selects the texts by their name.
Arguments
Callback Function Parameters
The callback function accepts three parameters:
$patternsstring[]The list of PCRE patterns. Each pattern matches one whole placeholder. Default: an empty array.$elementThe job element of the field. Itsfield_typeproperty names the field, for examplefield-my_email_text-0for the custom fieldmy_email_text. When the field belongs to a translation job, itsjob_idproperty is the ID of the job.$extra_dataarrayThe label, type and group of the field.
Example Usage
// Protect placeholders like {first_name} in the custom field my_email_text. add_filter( 'wpml_tm_xliff_unit_placeholders', function( $patterns, $element ) { if ( 'field-my_email_text-0' === $element->field_type ) { $patterns[] = '/\{[a-z_]+\}/'; } return $patterns; }, 10, 2 );