Skip to content

Instantly share code, notes, and snippets.

@srdjan-jcc
Created October 24, 2018 12:33
Show Gist options
  • Save srdjan-jcc/411473e2905145eeff4d10049ba5e1d7 to your computer and use it in GitHub Desktop.
Save srdjan-jcc/411473e2905145eeff4d10049ba5e1d7 to your computer and use it in GitHub Desktop.
Enhanced Media Library and WPML sync terms
<?php
/*
Plugin Name: Enhanced Media Library and WPML
Description: Synchronizes terms
Version: 0.0.1
*/
add_action( 'set_object_terms', 'wpml_eml_sync_terms', 10, 6 );
function wpml_eml_sync_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
if ( $taxonomy !== 'media_category' ) {
return;
}
$trid = apply_filters( 'wpml_element_trid', null, $object_id, 'post_attachment' );
if ( ! $trid ) {
return;
}
$attachments = apply_filters( 'wpml_get_element_translations', array(), $trid, 'post_attachment' );
if ( ! $attachments ) {
return;
}
foreach ( $attachments as $language_code => $attachment ) {
if ( (int) $attachment->element_id !== $object_id ) {
$translated_terms = array();
foreach ( $terms as $term_id ) {
$translated_term_id = apply_filters( 'wpml_object_id', $term_id, $taxonomy, false, $language_code );
if ( $translated_term_id ) {
$translated_terms[] = $translated_term_id;
}
}
remove_action( 'set_object_terms', 'wpml_eml_sync_terms', 10, 6 );
wp_set_object_terms( $attachment->element_id, $translated_terms, $taxonomy, false );
add_action( 'set_object_terms', 'wpml_eml_sync_terms', 10, 6 );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment