WPML, multi-language support for WordPress
If you ever want to build a multi-language WordPress website there is IMHO just one working solution and that is to use the WPML plugin. With WPML is relatively easy to build complex multi-language websites with all kind of options for the translation of not only posts and pages but also for plugins and themes.
The only thing what can be somewhat confusing for users/contributors is the back-end interface for creating posts and pages. When using the standard WordPress environment, a post will only be created in the default language and therefore its content will appear only on a part of the site. WPML does of course have extensive capabilities for posting in multiple languages but users will have to remember to tick a number of extra boxes to make this happen. In short some extra steps are required to make sure that post and pages appear in all the different languages the site supports.
Easy multi-language posts via a front-end form
To make it somewhat easier for users to add post to their sites I have set up a front-end form for them and with a little help from WPML support (these guys really know their stuff) I have created some PHP code which enhances the standard wp_insert_post() call. With this enhancement, content will be created in all the languages you want. In my case in just the two languages (english and french) I needed, but you can of course add as many languages you want. In effect you create a post per language, in my case with exact the same content, and then tie them together in the WPML tables based on the trid (translation id) from the original post that will connect the translation to the original. And if you want specific content per language you can just do that by changing the value of $my_content for the different versions of the post.
Enjoy…
The PHP snippet
$my_post_status = 'publish'; // 'draft' | 'publish' | 'pending'| 'future' | 'private' | 'custom_registered_status' ==> set the status of the new post
$post = array( //our wp_insert_post args
'post_title' => wp_strip_all_tags($title),
'post_content' => $my_content,
'post_status' => $my_post_status,
'post_type' => $my_post_type,
'post_date' => $date,
'post_date_gmt' => $date);
$new_post = wp_insert_post($post); //send our post, save the resulting ID
$_POST['icl_post_language'] = $language_code = 'en'; // change the language code
wpml_update_translatable_content( 'post_'.$my_post_type, $new_post, $language_code ); // update language code
$trid = wpml_get_content_trid('post_'.$my_post_type, $new_post); // get trid
$src_language_code = $_POST['icl_post_language'];
// round two for the french side of the site
$post['post_title'] = $title . ' fr'; // add fr to post_title
$new_post = wp_insert_post($post); //send our post the second time, save the resulting ID
$_POST['icl_post_language'] = $language_code = 'fr'; // change the language code
global $sitepress;
$language_code = 'fr'; // change the language code
$sitepress->set_element_language_details($new_post, 'post_'.$my_post_type, $trid, $language_code, $src_language_code);
natyca says
Hi!
Very nice post, I have been looking for a way to do this and had no luck. One quick question, I’m having some problems when setting a condition for the main language: the content field does not get populated on the initial language.
Here is what i mean:
1. uploading post on the english site,
1.1 post gets published in english but without content, language is set to english.
2. post gets duplicated to spanish,
2.1 post gets published in spanish and language is set to spanish.
2.2 post has content from english post, which is fine.
Thanks!
hnzz says
This piece of code does exactly that:
– You have written your post via a form or whatever means you like.
– You put the content in $my_content and the title in $my_title.
– You then post the stuff, grab the $trid post the stuff again with another language code and connect it to the orginal via the $trid saved earlier.
It works here 🙂