Last active
January 23, 2017 19:38
-
-
Save aconanlai/f4844657cf2311f212fa8fd8688278c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require(dirname(__FILE__) . '/wp-load.php'); | |
require(dirname(__FILE__) . '/wp-content/plugins/sitepress-multilingual-cms/sitepress.php'); | |
function __update_post_meta( $post_id, $field_name, $value = '' ) | |
{ | |
if ( empty( $value ) OR ! $value ) | |
{ | |
delete_post_meta( $post_id, $field_name ); | |
} | |
elseif ( ! get_post_meta( $post_id, $field_name ) ) | |
{ | |
add_post_meta( $post_id, $field_name, $value ); | |
} | |
else | |
{ | |
update_post_meta( $post_id, $field_name, $value ); | |
} | |
}; | |
function my_insert_posts($slice) { | |
$output = array(); | |
$custom_tax = array( | |
'keyword' => $slice['keywords'] | |
// , | |
// 'date_de_debut' => date('2010-02-23 18:57:33'), | |
// 'date_de_fin' => date('2010-02-25 18:57:33') | |
); | |
// Create original post object | |
$my_original_post = array( | |
'post_title' => $slice['titlefrench'], | |
'post_content' => $slice['bodyfrench'], | |
'post_status' => 'publish', | |
'post_author' => 1, | |
'post_type' => 'events', | |
'tax_input' => $custom_tax | |
); | |
// Create translation post object | |
$my_translated_post = array( | |
'post_title' => $slice['titleenglish'], | |
'post_content' => $slice['bodyenglish'], | |
'post_status' => 'publish', | |
'post_author' => 1, | |
'post_type' => 'events', | |
'tax_input' => $custom_tax | |
); | |
// Insert the 2 posts into the database | |
$original_post_id = wp_insert_post( $my_original_post ); | |
$translated_post_id = wp_insert_post( $my_translated_post ); | |
__update_post_meta( $original_post_id, 'google_maps_link', 'google map french link here' ); | |
__update_post_meta( $translated_post_id, 'google_maps_link', 'google map english link here' ); | |
__update_post_meta( $original_post_id, 'date_de_debut', '20100223' ); | |
__update_post_meta( $translated_post_id, 'date_de_debut', '20100223' ); | |
__update_post_meta( $original_post_id, 'date_de_fin', '20100223' ); | |
__update_post_meta( $translated_post_id, 'date_de_fin', '20100223' ); | |
return $output = array( | |
'original' => $original_post_id, | |
'translation' => $translated_post_id | |
); | |
} | |
function element_connect_on_insert($slice) { | |
$inserted_post_ids = my_insert_posts($slice); | |
if ( $inserted_post_ids) { | |
// https://wpml.org/wpml-hook/wpml_element_type/ | |
$wpml_element_type = apply_filters( 'wpml_element_type', 'events' ); | |
// get the language info of the original post | |
// https://wpml.org/wpml-hook/wpml_element_language_details/ | |
$get_language_args = array('element_id' => $inserted_post_ids['original'], 'element_type' => 'events' ); | |
$original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args ); | |
$set_language_args = array( | |
'element_id' => $inserted_post_ids['translation'], | |
'element_type' => $wpml_element_type, | |
'trid' => $original_post_language_info->trid, | |
'language_code' => 'en', | |
'source_language_code' => $original_post_language_info->language_code | |
); | |
do_action( 'wpml_set_element_language_details', $set_language_args ); | |
} | |
} | |
$slices = json_decode(file_get_contents('eventspls.json'), true); | |
if ($slices) { | |
foreach ($slices as $slice) { | |
element_connect_on_insert($slice); | |
}} else { | |
echo 'error'; | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment