Home › Toolset Professional Support › [Resolved] Access custom checkboxes field $_POST['wpcf-slug'] as an array
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Our next available supporter will start replying to tickets in about 5.66 hours from now. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
Tagged: Content-submission forms, CRED API, Toolset Forms
Related documentation:
This topic contains 11 replies, has 2 voices.
Last updated by Lara 3 years, 9 months ago.
Assisted by: Minesh.
Tell us what you are trying to do?
I try to access in a "cred_save_data" hook the value's of a custom checkboxes field $_POST['wpcf-select-weekday'] as an array.
I need to check, which checkboxes are checked through in_array ()
$startday = $_POST['wpcf-firstday']['datepicker']; $nextday = $startday + ( 1 * 24 * 60 * 60 ); $endday = $_POST['wpcf-lastday']['datepicker']; $weekdays = $_POST['wpcf-select-weekday']['value']; for ($day = $nextday; $day <= $endday; $day+=86400) { $weekday = date("N", $day ); if (in_array($weekday, $weekdays)) { // custom code here } }
Is there any documentation that you are following?
no
Is there a similar example that we can see?
no
What is the link to your site?
Site under development
Hello. Thank you for contacting the Toolset support.
Checkboxes fields are special fields and the value of checkboxes fields are saved as serialized array.
Can you please share problem URL where you added your form as well as access details and tell me how exactly you setup the fields and what fields you want to save.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Can you please share problem URL where you added your form as well as access details and tell me how exactly you setup the fields and what fields you want to save.
I have a CRED from (orange button) with which the user can create events. If the same event should be repeated multiple times, the user can insert a "lastday" and he can also select on which days the event should take place (the checkboxes field).
For the weekdays I stored the following values in the checkboxe custom field:
Title: Monday
Value to store: 1
Title: Tuesday
Value to store: 2
Title: Wednesday
Value to store: 3
.. and so on, until ..
Title: Sunday
Value to store: 7
The PHP code in Toolset Custom Code Snippet section:
(the name of the snippet is "schwarzesbrett_serien-termine")
add_action('cred_save_data', 'terminserie_erstellen',10,2); function terminserie_erstellen ($post_id, $form_data) { // if a specific form $forms = array( 10441 ); if (in_array($form_data['id'], $forms) && ($_POST['wpcf-eine-information-oder-ein-termin']['select'] == "1")) { $id_reitanlage = $_POST['reitanlagen_id']; $startday = $_POST['wpcf-rask-von-tag']['datepicker']; $nextday = $startday + ( 1 * 24 * 60 * 60 ); $endday = $_POST['wpcf-rask-bis-tag']['datepicker']; for ($day = $nextday; $day <= $endday; $day+=86400) { $weekday = date("N", $day ); $weekdays = $_POST["wpcf-rask-wochentag"]; if (in_array($weekday, $weekdays)) { // Create post object $termin = array( 'post_title' => $day, 'post_status' => 'publish', 'post_type' => 'event', 'meta_input' => array ( 'wpcf-rask-von-tag' => $day, 'wpcf-rask-bis-tag' => $day, 'wpcf-covid-19-journal-name-der-aktivitat' => $_POST['wpcf-covid-19-journal-name-der-aktivitat'] . $weekdays , 'wpcf-rask-welches-journal-termine' => $_POST['wpcf-rask-welches-journal-termine'], 'wpcf-eine-information-oder-ein-termin' => '1', ), ); // Insert the post into the database $termin_id = wp_insert_post( $termin ); // Connect with reitanlage if($id_reitanlage){ toolset_connect_posts( $relationship = 'reitanlage-event', $id_reitanlage, $termin_id);} } } } }
If I use a fictional array $weekdays = array("1" , "3" , "6"); instead of $weekdays = $_POST["wpcf-rask-wochentag"]; the duplicate will be created. That's why I try to figure out, how I can turn the $_POST["wpcf-rask-wochentag"] into an array.
At the moment the fictional array $weekdays = array("1" , "3" , "6"); is implemented in the testsite.
Kind regards,
Lara
I do not able to see the orange button on the frontend. Can you please share on what URL I can see that button?
Ups sorry. I fixed the problem.
1. log-in through hidden link /wp-admin
2. Go to hidden link
Then you should see it
If I use a fictional array $weekdays = array("1" , "3" , "6"); instead of $weekdays = $_POST["wpcf-rask-wochentag"]; the duplicate will be created. That's why I try to figure out, how I can turn the $_POST["wpcf-rask-wochentag"] into an array.
==>
Can you please tell me in what login it will have duplicates?
What values you expect to be assigned to $_POST["wpcf-rask-wochentag"]?
Sorry I forgot to translate this field. $_POST["wpcf-rask-wochentag"] ---> After the translation it says $_POST["wpcf-rask-weekday"]
For better understanding I attached you a picture, where you can see this custom fields.
It's a calendar. In the calender people can publish events. Each event can have a firstday and a lastday. Furthermore each event can have various weekdays. For example a event can have "January 26th 2021" as firstday and "February 5th 2021" as last day. The weekdays should be "Tuesday" and "Friday". So the event will be performed on "January 26th" a Tuesday, on "January 29th" a Friday, on "February 2th" a Tuesday and on "February 5th" a Friday.
The event that will be performed on "January 26th" should be independent from the events "January 29th", "February 2th" and "February 5th". Each event should have it's own WordPress event post. So that changing one of those WordPress posts later, will not affect the other posts. For example a week after the creation of the event series, someone decides, that the event "February 2th" will have a different starttime - it's not a problem. The "February 2th" can be changed but all other events stay the same.
In the post creation there is a loop that iterates from the firstday of an event over all days until it reaches the lastday of that event. Every time when the weekday of the current day in the loop is equal zu a weekday in the $_POST["wpcf-rask-weekday"] checkboxes field, a clone of the current WordPress event post should be created through ...
$termin_id = wp_insert_post( $termin );
... and ....
// Create post object $termin = array( 'post_title' => $day, 'post_status' => 'publish', 'post_type' => 'event', 'meta_input' => array ( 'wpcf-rask-von-tag' => $day, 'wpcf-rask-bis-tag' => $day, 'wpcf-covid-19-journal-name-der-aktivitat' => $_POST['wpcf-covid-19-journal-name-der-aktivitat'] . $weekdays , 'wpcf-rask-welches-journal-termine' => $_POST['wpcf-rask-welches-journal-termine'], 'wpcf-eine-information-oder-ein-termin' => '1', ), );
... is the clone / duplicate of the current WordPress event post with the exception, that it has a different date than the original post eg. January 29th
The the current WordPress event post in this case is the WordPress event post, that is created after I submit the cred form (in this case the event with the firstday "January 26th"
That you for all detailed explanation but I would like to know what is not working exactly where?
I need the information stored in this field $_POST["wpcf-rask-weekday"] in a structure / formation like this: array("1" , "3" , "6").
This is where I am stuck.
If the user checks "Monday" and "Tuesday" in the $_POST["wpcf-rask-weekday"] checkboxes field, it should be: array("1" , "2").
If the user checks "Tuesday", "Wednesday" and "Sunday" in the $_POST["wpcf-rask-weekday"] checkboxes field, it should be: array("2" , "3", "7") .. and so on
Can you please try to replace the following line of code:
$weekdays = $_POST["wpcf-rask-wochentag"];
With the following code:
$saved_weekdays = get_post_meta($post_id,'wpcf-rask-weekday',true); $weekdays = array(); foreach($saved_weekdays as $k=>$v): $weekdays[] = $v[0]; endforeach;
Now, the $weekdays will have 1,2,3 as per the selected checkboxes value.
Thanks Minesh. You hit the nail on the head.
Just in case, that anyone will struggle with a similar problem in the future, I will share the correct code...
add_action('cred_save_data', 'terminserie_erstellen',10,2); function terminserie_erstellen ($post_id, $form_data) { // if a specific form $forms = array( 10441 ); if (in_array($form_data['id'], $forms) ) { // the startday and the endday of your event $startday = $_POST['wpcf-rask-von-tag']['datepicker']; $nextday = $startday + ( 1 * 24 * 60 * 60 ); $endday = $_POST['wpcf-rask-bis-tag']['datepicker']; // loop that iterates through all days for ($day = $nextday; $day <= $endday; $day+=86400) { // the weekday of the current day in the loop $weekday = date("N", $day ); // the custom checkboxes field, that contain the weekdays of the event e.g. monday, tuesday //checkboxes setup: from Title: monday and value to store: 1 to Title: sunday and value to store 7 $saved_weekdays = get_post_meta($post_id,'wpcf-rask-weekday',true); $weekdays = array(); foreach($saved_weekdays as $k=>$v): $weekdays[] = $v[0]; endforeach; if (in_array($weekday, $weekdays)) { // Create a new post object. A clone / duplicate of the current post. Change the code according to your needs $termin = array( 'post_title' => $day, 'post_status' => 'publish', 'post_type' => 'event', 'meta_input' => array ( 'wpcf-rask-von-tag' => $day, 'wpcf-rask-bis-tag' => $day, 'wpcf-covid-19-journal-name-der-aktivitat' => $_POST['wpcf-covid-19-journal-name-der-aktivitat'] , 'wpcf-rask-welches-journal-termine' => $_POST['wpcf-rask-welches-journal-termine'], 'wpcf-eine-information-oder-ein-termin' => '1', ), ); // Insert the newly created post into the database $termin_id = wp_insert_post( $termin ); // If you need to connect the newly created post with another post in a many-to-many-relationship. // Otherwise just delete this piece of code // id of the other post $id_reitanlage = $_POST['reitanlagen_id']; // Create connection between both posts if($id_reitanlage){ toolset_connect_posts( $relationship = 'reitanlage-event', // relationship slug $id_reitanlage, // id of the other post $termin_id);} // id of the newly created post } } } }
My issue is resolved now. Thank you!