Last active
April 19, 2018 08:11
-
-
Save hwkdev/e096daafb4f0bfebe08664841e3a7353 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| ini_set('memory_limit', '-1'); | |
| ini_set('display_errors', 1); | |
| ini_set('display_startup_errors', 1); | |
| error_reporting(E_ALL); | |
| require_once('../wp-load.php'); | |
| // Vérification de l'utilisateur | |
| if(!current_user_can('administrator')) | |
| wp_redirect(home_url()); | |
| function csv_to_array($filename = '', $delimiter = ';'){ | |
| if(!file_exists($filename) || !is_readable($filename)) | |
| return false; | |
| $header = null; | |
| $data = array(); | |
| if(($handle = fopen($filename, 'r')) !== FALSE){ | |
| $i=0; while(($row = fgetcsv($handle, 0, $delimiter)) !== FALSE){ | |
| if(!$header){ | |
| $header = $row; | |
| }else{ | |
| $p=0; | |
| foreach($header as $head){ | |
| $head = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $head); | |
| $head = str_replace('"', '', $head); | |
| $data[$i][$head] = $row[$p]; | |
| $p++; | |
| } | |
| $i++; | |
| } | |
| } | |
| fclose($handle); | |
| } | |
| return $data; | |
| } | |
| function csv_to_json($input){ | |
| if($array = csv_to_array($input) . '.csv') | |
| $json = json_encode($array); | |
| file_put_contents($input . '.json', $json); | |
| return; | |
| } | |
| function json_to_array($file){ | |
| $array = json_decode(file_get_contents($file . '.json'), true); | |
| if(empty($array)) | |
| return false; | |
| return $array; | |
| } | |
| // Generate JSON from csv | |
| csv_to_json('fichier'); | |
| // Get JSON & process | |
| if($rows = json_to_array('fichier')){ | |
| $i=0; foreach($rows as $row){ | |
| // Only 200 first | |
| if($i > 200) | |
| break; | |
| $post_id = wp_insert_post(array( | |
| 'post_title' => $row['title'], | |
| 'post_name' => sanitize_title($row['title']), | |
| 'post_content' => '', | |
| 'post_type' => 'post_type', | |
| 'post_status' => 'publish', | |
| 'post_author' => 1, | |
| )); | |
| if(!$post_id) | |
| echo 'Import failed.'; | |
| // update_post_meta(); | |
| // update_field(); | |
| // wp_insert_term($term, $taxonomy, $args = array()); | |
| // if(term_exists($term, $taxonomy, $parent)) | |
| // wp_set_object_terms($object_id, $terms, $taxonomy, $append); | |
| $i++; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment