• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • sitesenkit
  • about me

Towards a Delicious Future

a blog by [email protected]

  • sitesenkit
  • about me
Home / Tech-notes / How to create multi-language front-end posts with WPML

How to create multi-language front-end posts with WPML

30/05/2013 by Hans Pronk

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.

Screen Shot 2013-05-31 at 2.56.59 PM

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_type = 'post'; // set post_type
$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);

About Hans Pronk

Ik hou me bezig met kunst, "alles internet", ICT en nieuwe media. Ik ben gefacineerd door de veranderingen die de wereld ondergaat, onder meer door de introductie van informatie technologie en alles wat daarmee samenhangt, en de de wijze waarop we daar (op micro- en macroschaal) mee omgaan

Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Lost & found

// stuff i found on the internet complete list on my microblog

  • Elasticsearch and Kibana are now business risks do change of license.

  • Apple en Google: wees gewaarschuwd

  • Ask Ethan: Is Light Fundamentally A Wave Or A Particle?

  • The Smalltalk Zoo

  • OtherUnix

  • Hitting the Books: How Bell Labs jump-started the multimedia art movement | Engadget

  • Introducing the Smalltalk Zoo - CHM

  • Macron promotes European tech ecosystem in an interview with Zennström | TechCrunch

Archives

Categories

Footer

  • top
  • home
  • about me
  • login

copyright © 2006–2021 [email protected] / sitesenkit