Skip to content

Instantly share code, notes, and snippets.

@sododesign
Created January 9, 2016 15:38
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 sododesign/154919eb3b52d5f0c91a to your computer and use it in GitHub Desktop.
Save sododesign/154919eb3b52d5f0c91a to your computer and use it in GitHub Desktop.
Translate ACF Media with WPML
<?php
/**
* Make Media attachments translatable with WPML
*
* Filter ACF images and galleries to switch attachment ids with their
* corresponding WPML translation.
*/
add_filter( 'acf/load_value/type=gallery', 'my_acf_load_translated_attachment', 10, 3 );
add_filter( 'acf/load_value/type=image', 'my_acf_load_translated_attachment', 10, 3 );
function my_acf_load_translated_attachment($value, $post_id, $field) {
$newValue = $value;
// Make sure we are using WPML
if ( function_exists('icl_object_id') ) {
// Galleries come in arrays
if ( is_array($value) ) {
$newValue = array();
foreach ($value as $key => $id) {
$newValue[$key] = icl_object_id($id, 'attachment');
}
}
// Single images arrive as simple values
else {
$newValue = icl_object_id($value, 'attachment');
}
}
return $newValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment