ナビゲーションをスキップする
availability:

WPML Version: 3.2

description:

Registers an individual text string for translation (as opposed to a string that is part of a package). This action is usually used for user input texts otherwise referred to as “dynamic text strings”.

For retrieving a string translation please see: wpml_translate_single_string
For registering a text string that is part of a package please see: wpml_register_string

Note: This hook requires the WPML String Translation module

type:
action
category:
Inserting Content
parameters:
do_action( 'wpml_register_single_string', string $context, string $name, string $value )
$context
(string) (Required) This value gives the string you are about to register a context. This will usually be the name of the plugin or theme, in a human readable format
$name
(string) (Required) The name of the string which helps the translator understand what’s being translated
$value
(string) (Required) The string that needs to be translated
$allow_empty_value
(bool: false by default) (Optional) Whether an empty value is allowed or not
$source_lang_code
(string) (Optional) The language code for the registered string
hook example usage:

Imagine you have created a custom widget with a title input field and some other options. You can register the widget title for translation using apply_filters but you also want to register the input text you can save in the widget’s other option fields.

Registering input texts for translation is best done when the widget options are saved or updated i.e. inside the ‘update’ method of widgets.

Below is an example of what the code could look like. Our example custom widget has a title, a single line input field and a text area.

Example

function update($new_instance, $old_instance){
	$instance = $old_instance;
	$instance['title'] 		= strip_tags($new_instance['title']);
	$instance['custom_input'] 	= strip_tags($new_instance['custom_input']);
	$instance['custom_textarea'] 	= $new_instance['custom_textarea'];

	//WPML
	/**
	 * register strings for translation
	 */
	do_action( 'wpml_register_single_string', 'Widgets', 'Custom Widget - input field', $instance['custom_input'] );
	do_action( 'wpml_register_single_string', 'Widgets', 'Custom Widget - textarea field', $instance['custom_textarea'] );
	//WPML

	return $instance;
}

The code above tells WPML that the user input text for the input field and textarea needs to be translated. The texts will be registered under the Widgets strings context with the name Custom Widget – input field and Custom Widget – textarea field respectively.