· 6 years ago · Dec 18, 2019, 12:54 AM
1/********************
2This function will allow you to import Events Manager Locations into your WordPress database.
3
4This function is VERY rough. I would strongly suggest doing some save testing by commenting out the actual database calls to prevent duplicate entries or even server overload!
5
6Most CSV files are comma-separated, but Mac's Numbers uses a ;
7You may need to edit line 22 for that.
8
9Exporting the current/old wp_em_locations table using phpMyAdmin will give you a great starter template.
10
11Please make sure you only run this function ONCE. There is no check wether or not the location already exists.
12*********************/
13
14function import_em_locations_from_csv() {
15 // Locate your comma-separated CSV file.
16 $file = plugin_dir_path( __FILE__ ) .'wp_em_locations.csv';
17
18 // Convert CSV file to usable array.
19 $imported = array();
20 $handle = fopen($file, "r");
21 if( empty($handle) === false ) {
22 while( ($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
23 $imported[] = $data;
24 }
25 fclose($handle);
26 }
27
28 // Test the imported array.
29 // This output will give you the array keys you will need to use a column numbers.
30 error_log( print_r( $imported, true) );
31
32 /*
33 * COMMENT OUT THE SECTION BELOW FOR TESTING!
34 */
35
36 // Let Events Manager create a new EM Location Object with values from the CSV.
37 // Note that the column index starts at 0. See the error_log output for more info.
38 foreach( $imported as $column ) {
39 $EM_Location = new EM_Location();
40 $EM_Location->location_slug = $column[3];
41 $EM_Location->location_name = $column[4];
42 $EM_Location->location_owner = 1; // ID of the WP Author (= User ID).
43 $EM_Location->location_address = $column[6];
44 $EM_Location->location_town = $column[7];
45 $EM_Location->location_state = $column[8];
46 $EM_Location->location_postcode = $column[9];
47 $EM_Location->location_region = $column[10];
48 $EM_Location->location_country = $column[11]; // ISO ALPHA-2 (all caps).
49 $EM_Location->location_latitude = $column[12];
50 $EM_Location->location_longitude = $column[13];
51 $EM_Location->post_content = $column[14];
52 $EM_Location->location_status = $column[15];
53
54 // Test the new EM_Locations Object before saving it to the database.
55 error_log( print_r( $EM_Location, true) );
56
57 // Let EM save to wp_em_locations, wp_posts & wp_postmeta.
58 // $EM_Location->save(); // Uncomment this line to let EM process live to the database.
59 }
60 return;
61}