I have a post types form built using Jetengine, the form it self will be available from the page managed by Elementor to handle, I wanted to make a quick check for the available data so the value of 'NIK' will be checked if it's already exist or not, and if it's existed it will show an alert pop up, i have tried some code to make the notification such as wp_send_json() and wp_die() but failed with json error, I don't really know what the problem is.
I'm not really good with wordpress, however with some help from chatgpt I'm able to make the code debug the error_log() to the debug.log, but still failed to call a pop up if the nik is existed, any help will be appreciated, here is the code.
function check_duplicate_post_on_elementor_submission( $data, $postarr ) {
if ( $data['post_type'] !== 'pemilih' ) {
error_log( 'Post Type Wrong' );
return $data;
}
error_log( 'Post Type Right' );
if ( isset( $_POST['fields'] ) ) {
$fields = json_decode( stripslashes( $_POST['fields'] ), true );
foreach ( $fields as $field ) {
if ( isset( $field['name'] ) && $field['name'] === 'nik' ) {
$custom_field_value = sanitize_text_field( $field['value'] );
break;
}
}
}
if ( empty( $custom_field_value ) ) {
error_log( 'Nik is empty' );
return $data;
}
error_log( 'NIK value: ' . $custom_field_value );
$args = array(
'post_type' => 'pemilih',
'meta_key' => 'nik',
'meta_value' => $custom_field_value,
'post_status' => array( 'publish', 'draft', 'pending', 'private' ),
'fields' => 'ids',
);
$existing_posts = get_posts( $args );
if ( !empty( $existing_posts ) ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
error_log( 'Nik is already in use' );
`die('failed');`
} else {
$referrer = wp_get_referer();
error_log( 'Nik is already in use' );
`die('failed');`
exit;
}
}
return $data;
}
add_filter( 'wp_insert_post_data', 'check_duplicate_post_on_elementor_submission', 10, 2 );