# Copy Custom Fields to All WPML Translations on Save

> Copies the custom fields you list from an original post to all of its WPML translations whenever the original is saved, for values like prices, SKUs or coordinates that never change between languages.

- Type: PHP
- Runs: Run everywhere
- Category: [Multilingual](https://wpide.com/library/category/multilingual/)
- Tags: wpml, custom-fields, sync
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/wpml-sync-custom-fields/

## Code

```php
$wpide_keys = array( 'price', 'sku', 'latitude', 'longitude' ); // Meta keys to copy.

add_action( 'save_post', function ( $post_id, $post ) use ( $wpide_keys ) {
    static $running = false;

    if ( $running || ! defined( 'ICL_SITEPRESS_VERSION' ) || wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    $type    = apply_filters( 'wpml_element_type', $post->post_type );
    $details = apply_filters( 'wpml_element_language_details', null, array( 'element_id' => $post_id, 'element_type' => $type ) );

    if ( ! $details || ! empty( $details->source_language_code ) || empty( $details->trid ) ) {
        return; // Not an original post.
    }

    $translations = apply_filters( 'wpml_get_element_translations', null, $details->trid, $type );

    if ( empty( $translations ) || ! is_array( $translations ) ) {
        return;
    }

    $running = true;
    foreach ( $translations as $translation ) {
        $target = (int) $translation->element_id;
        if ( $target === (int) $post_id ) {
            continue;
        }
        foreach ( $wpide_keys as $key ) {
            if ( metadata_exists( 'post', $post_id, $key ) ) {
                update_post_meta( $target, $key, get_post_meta( $post_id, $key, true ) );
            } else {
                delete_post_meta( $target, $key );
            }
        }
    }
    $running = false;
}, 20, 2 );
```

## Notes

Only runs when saving the original (source-language) post. For fields WPML already manages, set them to "Copy" in WPML > Settings > Custom Fields Translation instead.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Copy Custom Fields to All WPML Translations on Save" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
