Created
October 29, 2014 07:41
-
-
Save vishalbasnet23/19c9fe14bcf90331fbbf to your computer and use it in GitHub Desktop.
Custom CSV importer and insert into post WordPress
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
| <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> |
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 | |
| 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