Guest User

Untitled

a guest
Apr 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('memory_limit', '-1');
  4. ini_set('display_errors', 1);
  5. ini_set('display_startup_errors', 1);
  6. error_reporting(E_ALL);
  7. require_once('../wp-load.php');
  8.  
  9. // Vérification de l'utilisateur
  10. if(!current_user_can('administrator'))
  11. wp_redirect(home_url());
  12.  
  13. function csv_to_array($filename = '', $delimiter = ';'){
  14. if(!file_exists($filename) || !is_readable($filename))
  15. return false;
  16.  
  17. $header = null;
  18. $data = array();
  19. if(($handle = fopen($filename, 'r')) !== FALSE){
  20. $i=0; while(($row = fgetcsv($handle, 0, $delimiter)) !== FALSE){
  21. if(!$header){
  22. $header = $row;
  23.  
  24. }else{
  25. $p=0;
  26. foreach($header as $head){
  27. $head = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $head);
  28. $head = str_replace('"', '', $head);
  29.  
  30. $data[$i][$head] = $row[$p];
  31. $p++;
  32. }
  33. $i++;
  34. }
  35.  
  36. }
  37. fclose($handle);
  38. }
  39. return $data;
  40. }
  41.  
  42. function csv_to_json($input){
  43. if($array = csv_to_array($input) . '.csv')
  44. $json = json_encode($array);
  45.  
  46. file_put_contents($input . '.json', $json);
  47. return;
  48. }
  49.  
  50. function json_to_array($file){
  51. $array = json_decode(file_get_contents($file . '.json'), true);
  52. if(empty($array))
  53. return false;
  54.  
  55. return $array;
  56. }
  57.  
  58. // Generate JSON from csv
  59. csv_to_json('fichier');
  60.  
  61. // Get JSON & process
  62. if($rows = json_to_array('fichier')){
  63. $i=0; foreach($rows as $row){
  64.  
  65. // Only 200 first
  66. if($i > 200)
  67. break;
  68.  
  69. $post_id = wp_insert_post(array(
  70. 'post_title' => $row['title'],
  71. 'post_name' => sanitize_title($row['title']),
  72. 'post_content' => '',
  73. 'post_type' => 'post_type',
  74. 'post_status' => 'publish',
  75. 'post_author' => 1,
  76. ));
  77.  
  78. if(!$post_id)
  79. echo 'Import failed.';
  80.  
  81. // update_post_meta();
  82. // update_field();
  83.  
  84. // wp_insert_term($term, $taxonomy, $args = array());
  85.  
  86. // if(term_exists($term, $taxonomy, $parent))
  87. // wp_set_object_terms($object_id, $terms, $taxonomy, $append);
  88.  
  89. $i++;
  90. }
  91. }
Add Comment
Please, Sign In to add comment