qTranslate-X : How to make alt tag attribute of featured image change language


When you need a free plugin that would make your WordPress site multilingual, qTranslate-X is a way to go. It works great, but you might encounter a problem when trying to have multiple language support for attributes of Featured Image also known as Post Thumbnails. If you try to insert qTranslate language tags in Title, Description, Caption, and most importantly Alternative Text, you might notice that nothing gets translated when the_post_thumbnail() is called.

This article will show you how to make those fields multilingual.

Featured Image
Setting Featured Image in Post Editor

First we will examine how to store multilingual text when no tabs are available to switch between languages. Then we will explore where those attributes of Featured Image are stored and finally we will use PHP code to extract that information and use it when calling the_post_thumbnail() function.

qTranslate Language code in featured image attributes

Add multilingual text into the attributes of a featured image

Currently, qTranslate-X (v3.4.68) does not add tabs for switching languages (also known as Language Switching Buttons or LSB) to fields of attributes for Featured Image, so we will have to insert qTranslate language tags ourself by wrapping our text with language tags. There are three forms of language tags that we can use.

The following tags are considered obsolete by qTranslate-X, but they still work:

<!--:en-->english text<!--:--><!--:de-->german text<!--:-->

Another issue with the above language tags is that the "Alt Text" field, when editing "Featured Image" cannot save them and they get stripped, so I suggest to avoid using <!–:xx–>… <!–:–> all together.

Instead, use language tags as shown below:

[:en]english text[:de]german text

or like this:

{:en}english text{:de}german text
Note: To learn more about language tags in qTranslate-X, check their Multilangual Fields page on their website.

Now that we know how to save language tags into attributes of Featured Image, we can focus on how to make them display correctly.

Location of Featured Image attributes in Database

Simply calling the_post_thumbnail won't do anything in regard to translating our text. We will need to extract data of attributes for Featured Image ourselves. But to do that, we also must first know where the data is stored.

Featured Image thumbnails are stored in post_type table, same as regular posts but with post type of attachment. Inside those posts we will find data for most of the attributes of the featured image, like description, title and caption. Information like "alt text" is stored in the wp_postmeta table.

Where each attribute is stored is shown below:

DB Table: post_type

Column in post_type Featured Image attribute stored
post_content Description
post_title Title
post_excerpt Caption

DB Table: wp_postmeta

meta_key column meta_value
_thumbnail_id ID of that featured image in the post_type table
_wp_attachment_image_alt Alt text

PHP code to add

In our example, we are going to use Alt Text and Title attributes for our Featured Image, but you could also include description and caption in the same way. Size of our thumbnail is set to 250x250. Adjust that size to your needs. You can also leave it to null.

Locate the the_post_thumbnail() call in your template and replace that line with the following code:

  $thumbnail_id    = get_post_thumbnail_id($post->ID);
  $thumbnail_image = get_post($thumbnail_id);
  $alt_text = __(get_post_meta($thumbnail_id , '_wp_attachment_image_alt', true));
  the_post_thumbnail( array(250, 250),array( 'alt' => $alt_text,'title'=>$title));

Line by line explanation of the above code:

  • Line 1

    $thumbnail_id    = get_post_thumbnail_id($post->ID);

    To get our thumbnail from post_type table, we need its ID and we get that by using get_post_thumbnail_id function.

  • Line 2

    $thumbnail_image = get_post($thumbnail_id);

    We use that ID in get_post function which returns single WP_Post object. This post object has post type of attachment and contains our Featured Image.

  • Line 3, 4

    $alt_text = __(get_post_meta($thumbnail_id , '_wp_attachment_image_alt', true));

    From $thumbnail_image we can extract most of the attributes. In our example we are only interested in Title and Alt Text. Unlike other attributes, "Alt Text" is stored in post_meta table. To grab that, we use get_post_meta function and specify meta key _wp_attachment_image_alt.

    Both Title and Alternative Text will contain unfiltered text in all the languages. We need qTranslate to filter them, so we use __(string $str) PHP function which returns a translated string of the current language.

  • Line 5

    the_post_thumbnail( array(150, 150),array( 'alt' => $alt_text,'title'=>$title));

    All that is left to do is insert the variables containing translated text into an array of attributes as attr parameter of the_post_thumbnail function. Generated img tag will contain both attributes used in the array containing the text for correct language.

I hope you found this article useful. Please drop a comment or consider sharing it on social networks.

This article was first published in 2013 for qTranslate plugin. It has been republished and updated for qTranslateX plugin, which is descendant of qTranslate.


  1. Katrina Isabelle
    August 28, 2013
  2. Igor Jurisic
    October 20, 2013
    • admin
      October 20, 2013

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.