Hello 🙂
I have created my own plugin that imports events from a simple html formular. Everything work as I want, except the address only gets imported ass location name. I need the address to go directly to the location address field. Please see my code below:
add_action(‘init’, function () {
if (isset($_POST[‘opret_event’])) {
$address = sanitize_text_field($_POST[‘address’]);
$date = sanitize_text_field($_POST[‘date’]);
$start_time = sanitize_text_field($_POST[‘start_time’]);
$end_time = sanitize_text_field($_POST[‘end_time’]);
try {
$start_dt = new DateTime(“$date $start_time”);
$end_dt = new DateTime(“$date $end_time”);
$start_ts = $start_dt->getTimestamp();
$end_ts = $end_dt->getTimestamp();
// Opret event
$event_id = wp_insert_post([
‘post_title’ => ‘Privat Loppemarked’,
‘post_content’ => ‘Adresse: ‘ . $address,
‘post_status’ => ‘draft’,
‘post_type’ => ‘ajde_events’,
]);
if ($event_id) {
// Tidsfelter
update_post_meta($event_id, ‘_start’, $start_ts);
update_post_meta($event_id, ‘_end’, $end_ts);
update_post_meta($event_id, ‘evcal_srow’, $start_ts);
update_post_meta($event_id, ‘evcal_erow’, $end_ts);
update_post_meta($event_id, ‘evcal_allday’, ‘no’);
update_post_meta($event_id, ‘evcal_hide_endtime’, ‘no’);
// Opret location term hvis den ikke eksisterer
$existing_location = term_exists($address, ‘event_location’);
if (!$existing_location) {
$new_location = wp_insert_term($address, ‘event_location’);
if (!is_wp_error($new_location)) {
$location_id = $new_location[‘term_id’];
} else {
$location_id = 0;
}
} else {
$location_id = $existing_location[‘term_id’];
}
// Tildel location til eventet via term_id
if ($location_id) {
wp_set_object_terms($event_id, $location_id, ‘event_location’);
}
// Opdater location meta-informationer korrekt
update_post_meta($event_id, ‘evcal_location_name’, $address); // Tilføjer location name korrekt
update_post_meta($event_id, ‘evcal_location_address’, $address); // Tilføjer location address korrekt
// Farve pĂĄ eventet
update_post_meta($event_id, ‘evcal_event_color’, ‘#f39c12’);
// Tilføj event type
wp_set_object_terms($event_id, ‘Privat loppemarked’, ‘event_type’, true);
// Bekræftelse og redirect
set_transient(‘eventon_form_message’, ‘Event oprettet!’, 10);
wp_redirect($_SERVER[‘REQUEST_URI’]);
exit;
} else {
set_transient(‘eventon_form_message’, ‘Fejl: kunne ikke oprette event’, 10);
wp_redirect($_SERVER[‘REQUEST_URI’]);
exit;
}
} catch (Exception $e) {
set_transient(‘eventon_form_message’, ‘Fejl i dato eller tidspunkt’, 10);
wp_redirect($_SERVER[‘REQUEST_URI’]);
exit;
}
}
});
BTW: I have updated the plugin with author setting:
add_action(‘init’, function () {
if (isset($_POST[‘opret_event’])) {
$address = sanitize_text_field($_POST[‘address’]);
$date = sanitize_text_field($_POST[‘date’]);
$start_time = sanitize_text_field($_POST[‘start_time’]);
$end_time = sanitize_text_field($_POST[‘end_time’]);
try {
$start_dt = new DateTime(“$date $start_time”);
$end_dt = new DateTime(“$date $end_time”);
$start_ts = $start_dt->getTimestamp();
$end_ts = $end_dt->getTimestamp();
// Opret event
$event_id = wp_insert_post([
‘post_title’ => ‘Privat Loppemarked’,
‘post_content’ => ‘Adresse: ‘ . $address,
‘post_status’ => ‘draft’,
‘post_type’ => ‘ajde_events’,
]);
if ($event_id) {
// Tidsfelter
update_post_meta($event_id, ‘_start’, $start_ts);
update_post_meta($event_id, ‘_end’, $end_ts);
update_post_meta($event_id, ‘evcal_srow’, $start_ts);
update_post_meta($event_id, ‘evcal_erow’, $end_ts);
update_post_meta($event_id, ‘evcal_allday’, ‘no’);
update_post_meta($event_id, ‘evcal_hide_endtime’, ‘no’);
// Opret location term hvis den ikke eksisterer
$existing_location = term_exists($address, ‘event_location’);
if (!$existing_location) {
$new_location = wp_insert_term($address, ‘event_location’);
if (!is_wp_error($new_location)) {
$location_id = $new_location[‘term_id’];
} else {
$location_id = 0;
}
} else {
$location_id = $existing_location[‘term_id’];
}
// Tildel location til eventet via term_id
if ($location_id) {
wp_set_object_terms($event_id, $location_id, ‘event_location’);
}
// Opdater location meta-informationer korrekt
update_post_meta($event_id, ‘evcal_location_name’, $address); // Tilføjer location name korrekt
update_post_meta($event_id, ‘evcal_location_address’, $address); // Tilføjer location address korrekt
// Farve pĂĄ eventet
update_post_meta($event_id, ‘evcal_event_color’, ‘#f39c12’);
// Tilføj event type
wp_set_object_terms($event_id, ‘Privat loppemarked’, ‘event_type’, true);
// Tildel forfatteren til “rasmusheise”
$user = get_user_by(‘login’, ‘rasmusheise’);
if ($user) {
// Opdater forfatteren til eventet
$update_post = [
‘ID’ => $event_id,
‘post_author’ => $user->ID,
];
wp_update_post($update_post);
} else {
// Fejl: Bruger findes ikke
set_transient(‘eventon_form_message’, ‘Fejl: Bruger “rasmusheise” findes ikke.’, 10);
wp_redirect($_SERVER[‘REQUEST_URI’]);
exit;
}
// Bekræftelse og redirect
set_transient(‘eventon_form_message’, ‘Event oprettet!’, 10);
wp_redirect($_SERVER[‘REQUEST_URI’]);
exit;
} else {
set_transient(‘eventon_form_message’, ‘Fejl: kunne ikke oprette event’, 10);
wp_redirect($_SERVER[‘REQUEST_URI’]);
exit;
}
} catch (Exception $e) {
set_transient(‘eventon_form_message’, ‘Fejl i dato eller tidspunkt’, 10);
wp_redirect($_SERVER[‘REQUEST_URI’]);
exit;
}
}
});