Skip to content

Instantly share code, notes, and snippets.

@sirtimid
Last active April 10, 2021 17:16
Show Gist options
  • Select an option

  • Save sirtimid/c8747dcd3bfba4f60693cb85b63819f3 to your computer and use it in GitHub Desktop.

Select an option

Save sirtimid/c8747dcd3bfba4f60693cb85b63819f3 to your computer and use it in GitHub Desktop.
Translate media automatically when using Polylang in Wordpress
<?php
if ( !function_exists( 'translate_all_media' ) ) {
function translate_all_media() {
global $polylang;
if(!$polylang) return;
// find languages
$langs = array();
foreach (pll_languages_list() as $lang){
if(pll_default_language() !== $lang){
$langs[] = $lang;
}
}
// find media
$posts = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'lang' => pll_default_language(),
));
$int = 0;
foreach ($posts as $post) {
// itterate over all langs
foreach ($langs as $new_lang) {
// Bails if the translations already exists
if ( ! $polylang->model->post->get_translation( $post->ID, $new_lang ) ) {
$int++;
// creates the translation
$polylang->posts->create_media_translation( $post->ID, $new_lang );
}
}
}
// notify admin
if($int > 0){
echo '<div class="notice notice-success is-dismissible"><p>';
echo 'Translated '.$int.' media';
echo '</p></div>';
}
}
add_action( 'admin_notices', 'translate_all_media' );
}
@Arty2
Copy link
Copy Markdown

Arty2 commented Oct 2, 2016

This a great little function! Have you noticed any issues using it?

@sirtimid
Copy link
Copy Markdown
Author

sirtimid commented Nov 1, 2016

nope! ;)

@Arty2
Copy link
Copy Markdown

Arty2 commented Feb 24, 2019

Latest version of Polylang seems to break this very useful script, haven't went down in locating why, but the error log is:

Fatal error: Uncaught Error: Call to undefined method PLL_Admin_Filters_Media::create_media_translation() in /www/wp-content/themes/aether/functions.php:366 Stack trace: #0 /www/wp-includes/class-wp-hook.php(286): translate_all_media('') #1 /www/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array) #2 /www/wp-includes/plugin.php(465): WP_Hook->do_action(Array) #3 /www/wp-admin/admin-header.php(276): do_action('admin_notices') #4 /www/wp-admin/plugins.php(459): require_once('/www...') #5 {main} thrown in /www/wp-content/themes/aether/functions.php on line 366

@7studio
Copy link
Copy Markdown

7studio commented Mar 29, 2019

I had recently the same issue ๐Ÿ˜”
You need to replace $polylang->filters_media->create_media_translation( $post->ID, $new_lang ); by $polylang->posts->create_media_translation( $post->ID, $new_lang );. I found the solution into the file /polylang-pro/modules/plugins/acf.php line 429 ๐Ÿ˜‰

@sirtimid
Copy link
Copy Markdown
Author

I had recently the same issue
You need to replace $polylang->filters_media->create_media_translation( $post->ID, $new_lang ); by $polylang->posts->create_media_translation( $post->ID, $new_lang );. I found the solution into the file /polylang-pro/modules/plugins/acf.php line 429

Thanks!

@paolosax
Copy link
Copy Markdown

paolosax commented Apr 10, 2021

Excellent!
I wonder why the "if(pll_default_language() !== $lang)" con is needed when populate the $langs array beforehand...
I've noticed that loading an image from inside a post in DEFAULT language, it works (i.e.: the media library updates with the loaded attachment post fro all the languages available); but if you load an image from a post of an ALTERNATIVE language (not DEFAULT), the media library doesn't not automatically translate.
However, knowing that, it works like a charm!

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