Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
Created October 29, 2014 07:41
Show Gist options
  • Select an option

  • Save vishalbasnet23/19c9fe14bcf90331fbbf to your computer and use it in GitHub Desktop.

Select an option

Save vishalbasnet23/19c9fe14bcf90331fbbf to your computer and use it in GitHub Desktop.
Custom CSV importer and insert into post WordPress
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
<?php
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
while ( ($data = fgetcsv($handle, 1000, ",")) !== FALSE ) {
$row++;
if( $row > 1 ) {
$project_insert_args = array(
'post_title' => $data[foo_title_index],
'post_type' => 'post',
'post_status' => $data[foo_status_index],
'post_author' => get_current_user_id(),
'post_content' => $data[foo_content_index]
);
$new_insert_post_id = wp_insert_post( $project_insert_args );
if( $new_insert_post_id ) {
add_post_meta($new_insert_post_id,'foo_meta',$data[foo_meta_index]);
}
}
}
fclose($handle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment