function ra_events_import_csv_shortcode() {
if (isset($_POST[‘submit’]) && !empty($_FILES[‘csv_file’][‘tmp_name’])) {
// Required for media_sideload_image() to work outside the admin context.
require_once(ABSPATH . ‘wp-admin/includes/media.php’);
require_once(ABSPATH . ‘wp-admin/includes/file.php’);
require_once(ABSPATH . ‘wp-admin/includes/image.php’);
$csv_file = $_FILES[‘csv_file’][‘tmp_name’];
$handle = fopen($csv_file, ‘r’);
$header = fgetcsv($handle, 5000, ‘,’); // skip header row
$imported = 0;
while (($col = fgetcsv($handle, 5000, ‘,’)) !== FALSE) {
// —- WP core fields (columns 0-4) —-
$post_title = $col[0];
$post_content = $col[1];
$post_excerpt = $col[2];
$post_status = !empty($col[3]) ? $col[3] : ‘publish’;
$post_date = !empty($col[4]) ? $col[4] : current_time(‘mysql’);
if (empty($post_title)) continue;
// —- Insert the post —-
$post_id = wp_insert_post(array(
‘post_title’ => $post_title,
‘post_content’ => $post_content,
‘post_excerpt’ => $post_excerpt,
‘post_type’ => ‘events’, // <-- change if your CPT slug differs
'post_status' => $post_status,
‘post_date’ => $post_date,
));
if (is_wp_error($post_id) || !$post_id) continue;
// —- JetEngine meta fields (columns 5-22) —-
update_post_meta($post_id, ‘event_start_date’, $col[5]);
update_post_meta($post_id, ‘event_end_date’, $col[6]);
update_post_meta($post_id, ‘event_location’, $col[7]);
update_post_meta($post_id, ‘event_address’, $col[8]);
update_post_meta($post_id, ‘event_is_virtual’, $col[9]);
update_post_meta($post_id, ‘event_virtual_link’, $col[10]);
update_post_meta($post_id, ‘event_registration_url’,$col[11]);
update_post_meta($post_id, ‘event_capacity’, $col[12]);
// $col[13] event_speakers and $col[14] event_sponsors are repeaters — skipped.
update_post_meta($post_id, ‘event_is_featured’, $col[15]);
update_post_meta($post_id, ‘event_status’, $col[16]);
update_post_meta($post_id, ‘event_cost’, $col[17]);
update_post_meta($post_id, ‘event_organizer’, $col[18]);
update_post_meta($post_id, ‘event_contact_email’, $col[19]);
update_post_meta($post_id, ‘ra_calendar_ics_url’, $col[20]);
update_post_meta($post_id, ‘ra_calendar_google_url’,$col[21]);
update_post_meta($post_id, ‘ra_calendar_outlook_url’,$col[22]);
// —- Featured image (column 23) —-
if (!empty($col[23])) {
$image_url = esc_url_raw($col[23]);
$attach_id = media_sideload_image($image_url, $post_id, $post_title, ‘id’);
if (!is_wp_error($attach_id)) {
set_post_thumbnail($post_id, $attach_id);
}
}
$imported++;
}
fclose($handle);
unlink($csv_file);
echo ‘
‘ . $imported . ‘ events imported successfully!
‘;
}
ob_start();
?>
Import Past Events
Related News