How to add qtranslate-support for custom post type? Just saving text with tags OR add flag switcher.

My Custom Post Type field:

add_meta_box('project_rolesid', 'Roles box', 'project_roles', 'portfolio', 'normal');
<textarea id="portfolio_project_roles" cols="80" rows="7" name="portfolio_project_roles">'.$roles.'</textarea>

Trying to add suport by adding line

add_action('portfolio_project_roles','qtrans_useCurrentLanguageIfNotFoundShowAvailable', 0);

in qtranslate_hooks.php, but no effect.


Using tags is pretty straightforward. All data in qTranslate is stored between special <!--:lang-->content<!--:--> tags, wherein lang is the code for the language, like en, ru, de, etc.

The post edit UI is altered via qTranslate's heavy JavaScript. The JavaScript parses the content for preset fields and creates new controls. Before saving the same JavaScript does the opposite, merges the UI fields and wraps them up into the tags above. This simple behavior can be replicated by injecting additional JavaScript from your custom post type code to split your custom field into as many languages as you need, then merge them together, using qtrans_integrate_*, qtrans_save and other existing odds and ends as a guide for your own code.

Again, this is very straightforward - split tags in your #portfolio_project_roles, for each language create #portfolio_project_roles_{lang} textareas and fill with split content, before saving merge back into one #portfolio_project_roles wrapping each content language payload into the tags.

When saved, your custom post type metadata in your database would look like this, for example: "portfolio_roles" => "<!--:en-->Vladimir - Project Lead<!--:--><!--:ru-->Владимир - куратор проекта<!--:-->". To display chunks of this data depending on the language, you have to use a couple of filters qTranslate provides, mainly: qtrans_useCurrentLanguageIfNotFoundShowAvailable and the like, or even qtrans_use directly.

<div id="content>
    <?php echo qtrans_useCurrentLanguageIfNotFoundShowAvailable(
                get_post_meta( get_the_ID(), 'portfolio_roles', true ) ) );

This will output the correct language split payload (don't forget to escape the output using esc_html).

A cleaner approach would be to use the meta filter and hook it over the qtrans_use* functions. I'm sure there are other interesting approaches that can be taken, but the above should provide you with the basic outline of how things work. Hope this helps.

| improve this answer | |
  • Awesome! Work's pretty well without esc_html – skywind May 7 '13 at 16:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.