Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to figure out how to create a translation for a post using the internal WPML API (inc/wpml-api.php)

I simply want to create a translation for post ID xx, set some content and publish it.

I've tried to play around with wpml_add_translatable_content but couldn't get it right. Unfortunately there is not much documentation available for this. The closest lead I found is this thread, but I couldn't reduce the code to what I need. It's also possible to do this by directly writing to the database, following WPML's table structure, but I want to use the API.

Any suggestions are welcome.

share|improve this question
    
the user of the thread you refer to wants to duplicate images, but that is now already possible with the new Media module. From your question I don't really understand why you would want to go the API way and not work directly with WPML... –  Piet Jun 16 '11 at 7:59
    
@Piet, on the thread I linked, I'm only interested in the part where he uses wpml_add_translatable_content (he creates translated attachments, which is basically te same a creating translated posts). I want to go with the API because I would like to generate translations from within the code, not the GUI. In the end I'm hoping to be able to use the code inside a plugin I'm working on. –  mike23 Jun 16 '11 at 8:23
    
ok I understand sort of. Have you tried to ask Amir or Mihai in the WPML forum? They usually support plugin developers who want to do the extra work to make their plugins suitable to work with WPML. –  Piet Jun 16 '11 at 8:30
    
@Piet, yes I've opened a thread on the support forum two weeks ago and we've been discussing. I'm waiting for a conclusive answer but there's no solution yet. I'm sure it'll eventually come, as their support is usually very good. In the meantime I thought I'd ask here, as there are many people using WPML as well as plugin developers. –  mike23 Jun 16 '11 at 8:46
    
yeah WPML support is pretty awesome. Your solution below looks great, thanks for posting it. –  Piet Jun 16 '11 at 10:59

1 Answer 1

up vote 3 down vote accepted

I came up with a function that does the job for now :

/**
 * Creates a translation of a post (to be used with WPML)
 *  
 * @param int $post_id The ID of the post to be translated.
 * @param string $post_type The post type of the post to be transaled (ie. 'post', 'page', 'custom type', etc.).
 * @param string $lang The language of the translated post (ie 'fr', 'de', etc.).
 *    
 * @return the translated post ID
 *  */
function mwm_wpml_translate_post( $post_id, $post_type, $lang ){

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

    // Define title of translated post
    $post_translated_title = get_post( $post_id )->post_title . ' (' . $lang . ')';

    // Insert translated post
    $post_translated_id = wp_insert_post( array( 'post_title' => $post_translated_title, 'post_type' => $post_type ) );

    // Get trid of original post
    $trid = wpml_get_content_trid( 'post_' . $post_type, $post_id );

    // Get default language
    $default_lang = wpml_get_default_language();

    // Associate original post and translated post
    global $wpdb;
    $wpdb->update( $wpdb->prefix.'icl_translations', array( 'trid' => $trid, 'language_code' => $lang, 'source_language_code' => $default_lang ), array( 'element_id' => $post_translated_id ) );

    // Return translated post ID
    return $post_translated_id;

}
share|improve this answer
1  
This is great but you should also use the 'element_type' column in your update's where clause... Either you may end-up with conflicts ;-) –  Jonathan Liuti Jan 19 '12 at 14:34
    
@JonathanLiuti Could you elaborate how to set the column and what would be the right element_type or how to get it? –  hugo der hungrige Aug 9 at 15:47
    
nope sorry, not using wordpress anymore and its far away in my memory ;) –  Jonathan Liuti Aug 10 at 10:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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