Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diogoterremoto/1d2e5bd432a6e9b02c3bbd90f8bff6bf to your computer and use it in GitHub Desktop.
Save diogoterremoto/1d2e5bd432a6e9b02c3bbd90f8bff6bf to your computer and use it in GitHub Desktop.
Copy WP-Stateless field (`sm_cloud`) to all WPML translations on a media post upload to GCS.
<?php
/**
* Copy WP-Stateless field (`sm_cloud`) to all WPML translations on a media post upload to GCS.
*/
add_filter("wp_stateless_media_synced", function($metadata, $attachment_id, $force, $args) {
global $sitepress;
$trid = $sitepress->get_element_trid($attachment_id);
$translations = $sitepress->get_element_translations($trid);
// Get WP-Stateless field
$cloud_meta = get_post_meta($attachment_id, "sm_cloud", true);
if ($cloud_meta) {
foreach ($translations as &$translation) {
$this_translation_post_id = $translation->element_id;
// Set field in other translations that are not the current one
if ($this_translation_post_id != $attachment_id) {
update_post_meta($this_translation_post_id, "sm_cloud", $cloud_meta);
}
}
}
return $metadata;
}, 10, 4);
@diogoterremoto
Copy link
Author

I've got very little knowledge of PHP and WordPress, so please be aware when using this in production environments.
Happy to hear if this can be improved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment