add_filter( 'wpml_pb_string_placeholder_patterns', callable $function_to_add, 10, 4 );
Description
This filter declares the placeholders in the texts of a page builder, for example [all-fields] in the email of a form widget. A placeholder is text that the page builder replaces with a value, so a translation must return it exactly as it is.
Each page builder text has a name. For Elementor, the name is <control>-<widget type>-<node id>, for example email_subject-form-1a2b3c4. Add an entry whose key is a PCRE pattern for the names of your texts, and whose value lists one PCRE pattern for each kind of placeholder.
When WPML sends such a text to the Advanced Translation Editor, it marks every match, and the translation comes back with the same placeholder, character for character. Automatic translation never changes it.
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 a field that is not a page builder text, for example a custom field, use the wpml_tm_xliff_unit_placeholders filter.
Arguments
Callback Function Parameters
The callback function accepts four parameters. WPML 5.2 passes only the first one:
$placeholder_patternsarrayThe declared placeholders. Each key is a PCRE pattern that matches the name of a page builder text. Each value is the list of PCRE patterns for the placeholders in those texts, where each pattern matches one whole placeholder. Default: an empty array.$namestringThe name of the text in the translation job. WPML 5.3.0 and later.$post_idintThe ID of the post that the text belongs to, or0when there is none. WPML 5.3.0 and later.$ownerarray|nullThe owner of the package that holds the text, when available. WPML 5.3.0 and later.
Example Usage
This is what WPML declares for the email fields of the Elementor Pro Form widget:
add_filter( 'wpml_pb_string_placeholder_patterns', function ( $patterns ) { $patterns['/^(?:email_(?:subject|content|from_name)(?:_2)?|redirect_to)-form-/'] = [ '/\[all-fields\]/', '/\[field[^\]]*id="\w+"[^\]]*\]/', ]; return $patterns; } );