Hello
for the translations of the image data I wrote this plug-in.
I really don’t know if this can create problems with other parts of polylang but on my local test site it seems to work.
<?php
/*
Plugin Name: polylangimagetags
Description: extention for img meta fields for polylang
* Add translation strings to image post meta
*
* @param $form_fields array, fields to include in attachment form
* @param $post object, attachment record in database
* @return $form_fields, modified form fields
*/
class Polylimg extends Polylang_Base {
function __construct() {
parent::__construct();
$polylang = new Polyimg();
if(!function_exists('get_languages_list')){
function get_languages_list($hide_empty = false) {
return get_terms('language', array('hide_empty'=>$hide_empty, 'orderby'=>'term_group' ));
}
}
}
}
function pll_the_post_thumbnail($size = 'post-thumbnail', $attr = ''){
$lang=pll_current_language();
$opt=get_option('polylang');
if($opt['default_lang']==$lang) {
echo get_the_post_thumbnail( null, $size, $attr );
}else{
(int) $id = get_post_thumbnail_id();
$attr=array(
'title'=> get_post_meta( $id, 'pll_img_tags_name_'.$lang, true),
'class' => "attachment-$size",
'alt' => get_post_meta($id, 'pll_img_tags_alt_'.$lang, true),
);
echo get_the_post_thumbnail( null, $size, $attr );
}
}
function pll_img_tags( $form_fields, $post ) {
global $polylang;
$lang=$polylang->get_languages_list();
$opt=get_option('polylang');
foreach ($lang as $language_name){
if( $language_name -> slug !== $opt['default_lang']){
$form_fields['pll_img_tags_name_'.$language_name -> slug ] = array(
'label' => 'Title '.$language_name -> name ,
'input' => 'text',
'value' => get_post_meta( $post->ID, 'pll_img_tags_name_'.$language_name -> slug , true ),
'helps' => 'Title in '.$language_name -> name
);
$form_fields['pll_img_tags_alt_'.$language_name -> slug ] = array(
'label' => 'Alt '.$language_name -> name ,
'input' => 'text',
'value' => get_post_meta( $post->ID, 'pll_img_tags_alt_'.$language_name -> slug , true ),
'helps' => 'Alt text in '.$language_name -> name
);
$form_fields['pll_img_tags_caption_'.$language_name -> slug ] = array(
'label' => 'Caption '.$language_name -> name ,
'input' => 'text',
'value' => get_post_meta( $post->ID, 'pll_img_tags_caption_'.$language_name -> slug , true ),
'helps' => 'Caption in '.$language_name -> name
);
}
}
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'pll_img_tags', 10, 2 );
/**
* Save values of image metadata in media uploader
*
* @param $post array, the post data for database
* @param $attachment array, attachment fields from $_POST form
* @return $post array, modified post data
*/
function pll_img_tags_save( $post, $attachment ) {
global $polylang;
$lang=$polylang->get_languages_list();
foreach ($lang as $language_name){
if( isset( $attachment['pll_img_tags_name_'.$language_name -> slug] ) )
update_post_meta( $post['ID'], 'pll_img_tags_name_'.$language_name -> slug, $attachment['pll_img_tags_name_'.$language_name -> slug] );
if( isset( $attachment['pll_img_tags_alt_'.$language_name -> slug] ) )
update_post_meta( $post['ID'], 'pll_img_tags_alt_'.$language_name -> slug, $attachment['pll_img_tags_alt_'.$language_name -> slug] );
if( isset( $attachment['pll_img_tags_caption_'.$language_name -> slug] ) )
update_post_meta( $post['ID'], 'pll_img_tags_caption_'.$language_name -> slug, $attachment['pll_img_tags_caption_'.$language_name -> slug] );
}
return $post;
}
add_filter( 'attachment_fields_to_save', 'pll_img_tags_save', 10, 2 );