I have created two datetime fields: Screenshot of fields.
I want to set up a validation rule to ensure that the end date cannot be earlier than the start date. To achieve this, I selected the “Remote” validation option for the end date field and provided the following URL:
https://glossar:8890/wp-admin/admin-ajax.php?action=validate_end_date,
which should trigger a function to validate the fields. However, it is not working as expected.
Since I am new to this, I wanted to ask if I am going about this the wrong way. How should I properly set up this validation? I also tried using the “Max Value” option and entered the start date field there, but that didn’t work either.
Any guidance would be greatly appreciated!
add_action('wp_ajax_validate_end_date', function () {
// Überprüfen, ob die Felder gesendet wurden
if (isset($_GET['veranstaltung_start_wdjv6gfd912']) && isset($_GET['veranstaltung_ende_1j8y62g4ts'])) {
$start_date = (int) $_GET['veranstaltung_start_wdjv6gfd912']; // Als Timestamp
$end_date = (int) $_GET['veranstaltung_ende_1j8y62g4ts']; // Als Timestamp
// Prüfen, ob das Enddatum vor oder gleich dem Startdatum liegt
if ($end_date > $start_date) {
echo 'true'; // Valid
} else {
echo 'false'; // Invalid
}
} else {
echo 'false'; // Fallback, falls Werte fehlen
}
wp_die();
});
Thanks!