Programmatically insert posts in WordPress and set language for WPML plugin

This might sound like crazy, but I had to import about 3-4000 entries from a non-WP site to a… well, WP site. The problem, however, was that the site was multilanguage, and half of these posts were in different language. Using WPML plugin sounds like a great idea since I used it already on several other sites and I had no problem at all ($80 well spent!).

However, even if the plugin is just great for adding new content, processing old content/imports with is a major PITA. Mostly because their support forum staff might not know what to say sometimes.

I guess anyone knows how to insert a WP post, right?

// it's important to assign the returned value - that is the ID of the newly inserted post - to a variable
$new_post = wp_insert_post( array(
  'post_title' => "Entry Title",
  'post_type' => 'my_post_type',
  'post_status' => 'publish' 
) );

But, if you want to programmatically insert a new post in a different language than default, you must use wpml_update_translatable_content(), wich is contained into wpml-api.php:

/**
 * Update translatable content in the WPML translations table
 *  
 * @since 1.3
 * @package WPML
 * @subpackage WPML API
 *
 * @param string $content_type Content type.
 * @param int $content_id Content ID.
 * @param string $language_code Content language code.
 *  
 * @return int error code
 *  */
function wpml_update_translatable_content($content_type, $content_id, $language_code){

I just love how @param are explained here…

Anyhow, in our functions.php (I actually included another file from functions.php, but for the sake of simplicity, let’s keep it this way), we must include wpml-api.php file:

if( isset( $_GET['run_my_awesome_importer'] ) && $_GET['run_my_awesome_importer'] == 1 ){
  include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
  $language_code = 'ko'; // change the language code
  wpml_add_translatable_content( 'post_my_post_type', $new_post, $language_code );
}

This should be great and just work! No?

NO!

You actually need to set language code as a $_POST variable, so your code will be:

if( $_GET['run_my_awesome_importer'] == 1 ){
  include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
  $_POST['icl_post_language'] = $language_code = 'ko'; // change the language code
  wpml_add_translatable_content( 'post_my_post_type', $new_post, $language_code );
}

I mean what the fuck? I bet you didn’t think about this, did you?

Enjoy!

9 Comentarii to “Programmatically insert posts in WordPress and set language for WPML plugin”

  1. Hey, I need to perform the similar task via cronjob, but I found that you though mention the wpml_update_translatable_content, but in sample code you use wpml_add_translatable_content. I try both and they didn’t link together. I have 3 language, Can you share more information/code on this. Thanks.

  2. Ionuț Staicu

    @Sumit: thanks for your comment. Unfortunately, I forgot in what project I used this solution, so I could not help you directly with code samples. However, if you explain a little more where are you stuck, maybe I can help you a little.

    Are you using cronjob or wp_cron?

  3. Hello Ionut, I used wp_insert_post to create posts, and they are created in default language. How can i create a post directly in another language? Thanks.

  4. Ionuț Staicu

    @Alex: please see this more up to date solution.

  5. I used mysql to update icl_translations fot that post, it works fine. But i will check your solution too. Thx for your promt answer.

  6. Hi, I hope you can help me.
    I came into your post here while searching how to export in different (satellite) WP multilingual sites some contents ,all of them coming from a main WP multilingual site.

    I thought your instructions here could help a lot but I’m struggling since I cannot make the wpml API available to my script.
    Whilst I’ve no problems with the “core” wp functions including wp-load.php in a an external file -later run by cron jobs- it seems that I’ve no access to the sitepress object and its functions including

    include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );

    (which is correctly included BTW).I’ve never tried something similar earlier, I’m not sure if it’s a mistake by me or if it’s simply not possible outside of WP… can you help?…
    thanks a lot for your time

  7. Hi Andrea. To be on the safe side, you could create a new php file and include it from functions.php:

    // in functions.php
    if( $_GET['is-faux-cron'] ){
      require('faux-cron.php');
      die();
    }
    

    Then you can call your cron on the yoururl.com/?is-faux-cron=1. This way you are absolutely sure that the whole WP environment is loaded.

    I’m not too sure how wp-load.php – included from external file – deals with plugins & co.

  8. Thanks, you saved my life…time! :)

Show trackbacks
  1. […] the most popular article I found and seems like only solution people have. Article can be read in http://www.iamntz.com/3424/frontend-developer/programmatically-insert-posts-in-wordpress-and-set-lan…/. Though it doesn’t seems to solve my […]

Leave a comment!

You can insert code snippets using BBcode:
[js].[/js] [html].[/html] [php].[/php] [css].[/css]
You can also use some HTML tags:
<blockquote>.</blockquote> <code>.</code> <a href="">.</a> <strong>.</strong> <em>.</em>