-
-
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' ); | |
} |
nope! ;)
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
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.php
line 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!
This a great little function! Have you noticed any issues using it?