Skip Navigation
availability:

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.

type:
filter
category:
Miscellaneous
parameters:
apply_filters( 'wpml_duplicate_generic_string', string $value_to_filter, string $target_language, array $meta_data )
$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_key if 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|false Whether 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
hook 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 );