WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[Plugin : Polylang] -How to set language information used by polylang ? (3 posts)

  1. ben.IT
    Member
    Posted 1 year ago #

    Hi,
    I am working on a wordpress instance which is feeded by XML-RPC. In this way, I can synchronize selected content between the two WordPress instances.

    This instance which is feeded by XML-RPC uses the Polylang plugin to manage 2 languages (English and French).

    My question is :
    How can I set using a script the language of a content which is imported by XML-RPC.

    This is what I tried :
    I look at the rows of the tables : wp3_posts,wp3_postmeta and tried to write a script to build the language information for a given post.
    I know Polylang can set a default language to content by I am not able to do it myself !

    For instance, the post with ID 23 has his language set to French (set in backoffice) whereas I don't succeed in setting the information language of the post ID #30 using my script.

    SELECT wp3_posts.*,wp3_postmeta.*
    FROM wp3_posts,wp3_postmeta
    where wp3_posts.id  in (23,30) and
    wp3_postmeta.post_id = wp3_posts.id
    23	1	2012-09-05 10:19:48	2012-09-05 08:19:48	contenu	article a synchroniser		publish	open	open		article-a-synchroniser-3			2012-09-05 10:21:36	2012-09-05 08:21:36		0	http://my_domainblog/2012/09/05/article-a-synchroniser-3/	0	post		0	62	23	_edit_lock	1346833298:1
    23	1	2012-09-05 10:19:48	2012-09-05 08:19:48	contenu	article a synchroniser		publish	open	open		article-a-synchroniser-3			2012-09-05 10:21:36	2012-09-05 08:21:36		0	http://my_domainblog/2012/09/05/article-a-synchroniser-3/	0	post		0	63	23	_edit_last	1
    23	1	2012-09-05 10:19:48	2012-09-05 08:19:48	contenu	article a synchroniser		publish	open	open		article-a-synchroniser-3			2012-09-05 10:21:36	2012-09-05 08:21:36		0	http://my_domainblog/2012/09/05/article-a-synchroniser-3/	0	post		0	65	23	_translations	s:33:"a:2:{s:2:"fr";i:23;s:2:"en";i:0;}";
    30	1	2012-09-05 11:17:14	2012-09-05 09:17:14	contenu	article a synchroniser		publish	open	open		article-a-synchroniser-10			2012-09-05 11:17:14	2012-09-05 09:17:14		0	http://my_domainblog/2012/09/05/article-a-synchroniser-10/	0	post		0	94	30	_translations	s:33:"a:2:{s:2:"fr";i:30;s:2:"en";i:0;}";
    30	1	2012-09-05 11:17:14	2012-09-05 09:17:14	contenu	article a synchroniser		publish	open	open		article-a-synchroniser-10			2012-09-05 11:17:14	2012-09-05 09:17:14		0	http://my_domainblog/2012/09/05/article-a-synchroniser-10/	0	post		0	95	30	_edit_last	1
    30	1	2012-09-05 11:17:14	2012-09-05 09:17:14	contenu	article a synchroniser		publish	open	open		article-a-synchroniser-10			2012-09-05 11:17:14	2012-09-05 09:17:14		0	http://my_domainblog/2012/09/05/article-a-synchroniser-10/	0	post		0	96	30	_edit_lock	1346836782:1

    I think that polylang must set extra informations in other tables but I don't know which ones and how !

    Does anybody can help me ?
    Greetings,
    Ben

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    The language is a taxonomy. So the link between a post and its language is in the table wp_term_relationships. wp_postmeta contains only the link between translations. Please notice that s:33:"a:2:{s:2:"fr";i:30;s:2:"en";i:0;}"; is the format for 0.8.x but it will change in 0.9 to a:2:{s:2:"fr";i:30;s:2:"en";i:0;} (the previous format is a result from a mistake).

  3. ben.IT
    Member
    Posted 1 year ago #

    Thanks for your answer Chouby.
    Inspecting Polylang code, I found what I want to set the default language of my posts sended via XML-RPC.
    I wrote the following function which is a snippet of polylang sources :

    <?php
    require('../wp-load.php');
    require_once('../wp-content/plugins/polylang/include/admin-base.php');
    require_once('../wp-content/plugins/polylang/include/admin.php');
    
    /*
    This is a hack from /wp-content/plugins/polylang/include/admin.php line 243 to 262
    * */
    function setDefaultLanguage ($defaultLanguage='fr') {
    	$polylang = new Polylang_Admin();
    	global $wpdb ;
    
    	$untranslated = $polylang->get_untranslated(); var_dump($untranslated);
    	$lang = $polylang->get_language($defaultLanguage); var_dump($lang);
    
    	$values = array();
    	foreach ($untranslated['posts'] as $post_id)
    	{$values[] = $wpdb->prepare("(%d, %d)", $post_id, $lang->term_taxonomy_id); }
    	echo 'values='; var_dump($values);
    
    	if ($values) {
    		echo 'ipmlode values : '.implode(',', $values) ;
    		$wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES " . implode(',', $values));
    		wp_update_term_count($lang->term_taxonomy_id, 'language'); // updating term count is mandatory (thanks to AndyDeGroo)
    	}
    
    	$values = array();
    	foreach ($untranslated['terms'] as $term_id)
    	{$values[] = $wpdb->prepare("(%d, %s, %d)", $term_id, '_language', $lang->term_id);}
    	echo 'values='; var_dump($values);
    
    	if ($values)
    	$wpdb->query("INSERT INTO $wpdb->termmeta (term_id, meta_key, meta_value) VALUES " . implode(',', $values));
    
    }
    
    setDefaultLanguage('fr');

    Ben

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic