Automatically create translation
-
Hi,
I’ve been trying to write a function to create translation post automatically when the default language post is saved/published. It is because I do not want a new translation will erase all custom field values as they are “synced”.
function publish_article_add_translate( $ID, $post ) { if (strpos($post->post_title,'auto added')===false) { $org_id=$post->ID; if (!pll_get_post($org_id,'en')){ $en_post=clone $post; $en_post->ID=''; $en_post->post_title.=' (EN auto added)'; $en_post_id=wp_insert_post( $en_post ); pll_set_post_language($en_post_id, 'en'); } else $en_post_id=pll_get_post($org_id,'en'); if (!pll_get_post($org_id,'zh-hans')){ $hans_post=clone $post; $hans_post->ID=''; $hans_post->post_title.=' (简 auto added)'; $zh_hans_post_id=wp_insert_post( $hans_post ); pll_set_post_language($zh_hans_post_id, 'zh-hans'); } else $zh_hans_post_id=pll_get_post($org_id,'zh-hans'); if (!pll_get_post($org_id,'zh-hant')){ $hant_post=clone $post; $hant_post->ID=''; $hant_post->post_title.=' (繁 auto added)'; $zh_hant_post_id=wp_insert_post( $hant_post ); pll_set_post_language($zh_hant_post_id, 'zh-hant'); } else $zh_hant_post_id=pll_get_post($org_id,'zh-hant'); $translationArray=array('en'=> $en_post_id ,'zh-hans' => $zh_hans_post_id, 'zh-hant' => $zh_hant_post_id); if (count($translationArray)==3) pll_save_post_translations($translationArray); } } // add_action( 'publish_fashion-article', 'publish_article_add_translate', 10, 2 );The code only work if I die() the script right after the pll_sve_post_translations() function. I can’t figure out why it’s not working, any one has a thought?
The topic ‘Automatically create translation’ is closed to new replies.