apply_filters( 'wpml_duplicate_generic_string', string $value_to_filter, string $target_language, array $meta_data )
- Type
- filter
- Category
- Miscellaneous
- WPML Version
- 3.2
Description
WPML applies this hook filter on custom field values, post content, post title and post excerpt when a WordPress post type is being duplicated and before the data is saved into the database. The data are in this way made available to external themes and plugins for filtering.
Arguments
- $value_to_filter
- (string) (Required) The data string to filter
- $target_language
- (string) (Optional) The target language code
- $meta_data
- (array) (Optional) See details below:
- context(string) The data context. Set to “custom_field” if the value belongs to a custom field and “post” for all other cases (post title, content or excerpt)
- attribute(string) Set to: “value” for custom fields, “content” for post content, “title” for post title, “excerpt” for post excerpt
- key(string) The
$post_meta->meta_keyif the filter is being applied to custom field values. Otherwise the id of the original post that acts as master for duplication - $is_serialized(bool) Passed only for custom field data.
true|falseWhether the data is serialized. This allows to unserialize the value if needed. - post_id(int) Passed only for custom field data. The id of the post being duplicated
- master_post_id(int) Passed only for custom field data. The id of the original post that acts as master for duplication
Example usage
A basic example
function my_override_post_duplication( $value_to_filter, $target_language, $meta_data ) {
$context = $meta_data[ 'context' ];
if ( $value_to_filter !== '' && $context ) {
$prefix = '';
$attribute = $meta_data[ 'attribute' ];
if ( $context == 'post' ) {
switch ( $attribute ) {
case 'content':
$prefix = '<h3>' . $target_language . '</h3>';
break;
case 'excerpt':
$prefix = $target_language . "nn";
break;
default:
$prefix = '[' . $target_language . '] ';
}
} elseif ( $context == 'custom_field' && $attribute == 'value' ) {
$is_serialized = $meta_data[ 'is_serialized' ];
if ( ! $is_serialized && is_string( $value_to_filter ) && ! is_numeric( $value_to_filter ) ) {
$prefix = '[' . $target_language . '] ';
}
}
if($prefix) {
$value_to_filter = $prefix . $value_to_filter;
}
}
return $value_to_filter;
}
add_filter( 'wpml_duplicate_generic_string', 'my_override_post_duplication', 10, 3 );
Related hooks
icl_ls_languages, wpml_sync_custom_field_copied_value, wpml_ls_enable_ajax_navigation, wpml_element_type, wpml_setting, wpml_sub_setting, wpml_exclude_post_from_auto_translate, wpml_forms_config_ninja-forms, wpml_forms_config_wpforms, wpml_tf_feedback_open_link