WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
Keeping images for translated posts (20 posts)

  1. felicino1968
    Member
    Posted 2 years ago #

    Hello,
    I am searching for the possibility to not only keep the featured image, but all attachments for the translated post, is there a way to do this?
    Of course the second step would be to translate also the image metadata ( tiltle, description, caption,..), this would avoid uploading the same picture again times for each language.
    Anybody has got an idea?
    thanks for your support
    felicino

    http://wordpress.org/extend/plugins/polylang/

  2. felicino1968
    Member
    Posted 2 years ago #

    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 );
  3. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    That's really nice to share this.

    For your information, I am currently working on this feature for Polylang v0.9 (planned for the end of the summer). However I use a much different way. A different attachment is created for each language (although the media itself is of course not duplicated) and the language is associated with a taxonomy (like it is for posts) instead of using post metas.

    Sorry that it will come too late for you.

  4. p4004
    Member
    Posted 1 year ago #

    Hello,

    I have Polylang Version 0.9.3 installed. Is the feature of having the same images for translatable post already implemented?

    I have post with many images attached. I need to translate it. If i click to add new translation, the images does not appear in the media uploader gallery of the translatable post.

  5. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    The reason for your problem is that you did not set any language for your images. And the media uploader shows only the media with the same language as the post you are currently editing.

    If you don't need to translate images captions and descriptions, just add the following line in your wp-config.php (as described at the end of the documentation):

    define(PLL_MEDIA_SUPPORT, false);
  6. p4004
    Member
    Posted 1 year ago #

    Hello once again,

    When editing post, I still crucially need to see all uploaded images to wordpress site no matter what post's language is. In other words, I need to use same images for posts across multiple languages.

    Googled everywhere but did not find a solution. Maybe I have missed something very obvious.

    Adding define('PLL_MEDIA_SUPPORT', false); does not help. Should it? It just disables posibility to choose image language. But image still does not appear at posts in different languages.

    I am using Polylang Version 0.9.8.

  7. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

  8. p4004
    Member
    Posted 1 year ago #

    Thank you, after upgrading I finally can see images for all posts no matter what their language is. Great! Thanks.

  9. gtsak
    Member
    Posted 1 year ago #

    Hello

    I am new to polylang and I have to say that it is one of the best plugins I have used. However I have a problem when I am trying to translate my media. The thing is that I haven't found how I can do it. I have declared the language for all my media and when I am pressing Add new for the second language, I get back the editing screen. Is there a more detailed guideline? Maybe it's so obvious that I can't see it.

    regards

    G

  10. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Yes you go to the editing screen, but you will notice that the language has changed. So you can enter the caption and so on in the new language.

  11. fotogenico
    Member
    Posted 1 year ago #

    yes, but then need to upload images again?

  12. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Of course not! Only one image is stored.

  13. gtsak
    Member
    Posted 1 year ago #

    So, the translation refers to the title and the alt text right? We don't link two images together, one for each language, right?

    G

  14. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Yes you can translate the title and the alt text, but the image is the same.

  15. fotogenico
    Member
    Posted 1 year ago #

    I still don't know how to do it...help.

  16. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    How to do what?

  17. fotogenico
    Member
    Posted 1 year ago #

    images of the translated post don't show up.

  18. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Could you better explain what you do and what is the result you expect?

  19. fotogenico
    Member
    Posted 1 year ago #

    Well, I thought that if I need just to translate my post ( not creating a new one ) my images attached to the original post will be visible in the translated post as well so I don't need to upload them again and again. If its not possible then I would expect at least that they show up in my media gallery.

  20. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    OK. You have two ways of working with images: either you need to translate it (caption, alternative text...) or not.

    1. In the first case you have to upload your images as usual. A language will be affected to a language. And you need to go to the media table to add a translation for each media you add. Only the images that you have translated are available for the translated post.

    2. If you don't need to translate the image caption and so on, then go to the Polylang settings and uncheck the media support. you will have no more language associated to your image and you will be able to use them as usual.

    Note that in all cases, there is currently no way to automatically copy your images from the original post constent to the translated post content.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic