Last active
May 24, 2021 14:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.