-
-
Save sirtimid/c8747dcd3bfba4f60693cb85b63819f3 to your computer and use it in GitHub Desktop.
| <?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' ); | |
| } |
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 ๐
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.phpline 429
Thanks!
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!
Latest version of Polylang seems to break this very useful script, haven't went down in locating why, but the error log is: