I am using a frontend form for appointment creation in combination with a script hooked by cred_save_data_(form ID). It has worked fine over months and it still does work fine, but now with the latest updates I face draft post entries in my database with a creation date about 3 - 4 seconds after (!) creation of the intended appointment post. I have no idea why or where this post creation might be triggered.
Link to a page where the issue can be seen:
issue appears in database. The form works fine and the post created by it is in good shape.
I expected to see:
only the intended appointment post. No additional draft post
Regards,
Sven
The cred_save_data script of mine:
<h1>
function g2TerminPersonNeu( $post_id, $form_data ){
$startdateUNIX = $_POST[ 'wpcf-startdate' ][ 'timestamp' ];
// $startdateUNIX = $startdate[ 'timestamp' ];
if ( empty( $startdateUNIX ) ) {
wp_delete_post( $post_id, true );
exit;
}
$enddate = $startdateUNIX + 3600;
$topic = $_POST[ 'topic' ];
$topic1 = maybe_unserialize( $topic );
if ( is_array( $topic1 ) && count( $topic1 ) > 1 ) {
$topic2 = implode( " | ", $topic1 );
}
else if ( is_array( $topic1 ) && count( $topic1 ) == 1 ) {
$topic2 = $topic1[0];
}
else {
$topic2 = $topic1;
}
$parentkartei = $_POST[ "connect-to-karteikopf" ];
// set wpfd subcategories for termin related upload files
$user_id = get_post_meta( $parentkartei, 'wpcf-mirror_id', true );
$subcat1 = get_user_meta( $user_id, 'besch_id', true );
$subcat2 = get_user_meta( $user_id, 'bef_id', true );
$subcat3 = get_user_meta( $user_id, 'rech_id', true );
// set termin post meta data
$first = get_user_meta( $user_id, 'first_name', true );
$last = get_user_meta( $user_id, 'last_name', true );
$fullname = $first . ' ' . $last;
$titleslug = strtolower( $first . '-' . $last );
$Datum = date( 'd.m.Y', $startdateUNIX );
$Zeit = date( 'H:i', $startdateUNIX );
$Title = 'Termin für ' . $last . ', ' . $first . ' gebucht am ' . $Datum . ' um '. $Zeit .' Uhr';
$values = array(
'ID' => $post_id,
'post_author' => 1,
'post_content' => $Title,
'post_status' => 'private',
'post_title' => $fullname,
'post_name' => $titleslug,
'post_type' => 'termin',
'meta_input' => array(
'wpcf-startdate' => $startdateUNIX,
'wpcf-enddate' => $enddate,
'wpcf-topic' => $topic2,
'wpcf-calid' => 'keine',
'wpcf-eterminid' => 'keine',
'wpcf-terminstatus' => 1
)
);
wp_update_post( $values, true, true );
$wpfd_cat = g2SetupWPDownload_Taxonomy( $post_id, $subcat3, $subcat1, $subcat2, 0);
$user_stuff = get_userdata( $user_id );
$user_stuff->add_cap( "can_access_{" . $post_id . "}", true );
// relate termin file to karteikopf parent
toolset_connect_posts( 'karteikopf-termin', $parentkartei, $post_id);
$term = get_the_terms( $parentkartei, 'firmakat');
$term1 = $term[ 0 ];
$term_id = $term1 -> term_id;
$employer = $term1 -> name;
wp_set_post_terms( $post_id, $term_id, 'firmakat');
//is termin for employer set too?
if ( $employer != '00000' ) {
// appointment date to german format without time
$probstart = $startdateUNIX;
$probstart = intval( $probstart );
$probstart = date( 'd.m.Y', $probstart );
// catch employer firma id via firmakat taxonomy term of current termin - karteikopf relationship
$args = array(
'post_type' => 'firma',
'post_status' => 'any',
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'wpcf-kundennummer',
'value' => $employer,
)
)
);
$firmaID = get_posts( $args );
$post2_id = $firmaID[ 0 ];
// firma --> karteikopf
$firmkopf = toolset_get_related_post( $post2_id, 'karteikopf-firma');
// karteikopf --> termine of employer
$args = array(
'query_by_role' => 'parent',
'return' => 'post_id',
);
$firmtermine = toolset_get_related_posts( $firmkopf, 'karteikopf-termin', $args );
$firmtermine = maybe_unserialize( $firmtermine );
foreach ( $firmtermine as $firmentermin ) {
$firmstart = get_post_meta( $firmentermin, 'wpcf-startdate', true );
$firmstart = intval( $firmstart );
$firmstart = date( 'd.m.Y', $firmstart );
if( $firmstart == $probstart ) {
$firmtopic = get_post_meta( $firmentermin, 'wpcf-topic', true );
update_user_meta( 1, 'termcheck1', $firmstart . ' // ' . $probstart . ' // ' . $firmtopic );
if ( $firmtopic == 'Untersuchungen'){
$firmtermcat = get_post_meta( $firmentermin, 'beschterm_id', true);
update_post_meta( $post_id, 'firmbeschtermid', $firmtermcat );
exit;
}
}
}
}
}
add_action('cred_save_data_7149','g2TerminPersonNeu',10,2);
</h1>