qtranslate / qtranslate-xt

qTranslate-XT (eXTended) - reviving qTranslate-X multilingual plugin for WordPress. A new community-driven plugin soon. Built-in modules for WooCommerce, ACF, slugs and others.
GNU General Public License v2.0
550 stars 103 forks source link

wp_insert_post along with setting a language #1297

Closed scooterlord closed 1 year ago

scooterlord commented 1 year ago

Hello and thanks for keeping this plugin alive!

Is there a way to create a post programmatically and set the language for that post?

herrvigg commented 1 year ago

What does "set the language for that post" mean? There's nothing special in DB beside the ML tags. So yes, you can create a ML post by setting the fields with ML tags. However you have to be consistent with the languages being activated. We cannot guarantee the behavior if something is written in a wrong format.

herrvigg commented 1 year ago

The official API is not entirely clear but there are some functions that you may use quite safely (not subject to change) to pack the tags from a dictionary with keys = lang_code and values = text for each lang:

$lang_text = [ 'en' => 'english text', 'fr' => 'texte francais', '<other>' => ... ];
$ml_text = qtranxf_join_b( $lang_text );

// this should give '[:en]english text[:fr]texte francais[<other>]...[:]'
// then use it for the post
$post->content = $ml_text;

Unfortunately there's no public function to retrieve the active languages. You can get the codes from $q_config['enabled_languages'] but that's not so great to use such global. Consider that as a hack until there's an official API.