Problem: I have two post types, Patients and Threads, in an M2M relationship. On the Patient single post, I would like to allow site Users to create a new Thread, and automatically relate it to the current Patient. I would also like to include information from the current Patient in the post body of the new Thread.
Solution: It's not currently possible to create a new post and related it to another post in an M2M relationship using only one Form, so you would have to use custom code.
1. Create the new Thread Form and modify the Form contents so that only the submit button is shown, and add a generic field to contain the current Patient ID:
[credform] [cred_field field="form_messages" class="alert alert-warning"] <div style="display:none;"> [cred_field field="post_title" class="form-control" output="bootstrap" value="Re: [wpv-post-title]"] [cred_field field='post_content' output='bootstrap' value="Re: [wpv-post-link]"] [cred_generic_field type='hidden' field='patient'] { "default":"[wpv-post-id]" } [/cred_generic_field] </div> [cred_field field="form_submit" output="bootstrap" value="Create Thread" class="btn btn-primary btn-lg"] [/credform]
2. Set up the Form to redirect to any other custom Page after submission.
3. Insert that View in your Template Layout for Patients.
4. Use the Forms API and the Post Relationships API to automatically connect the new Thread to the current Patient:
add_action('cred_save_data', 'cred_link_patient_and_thread',10,2); function cred_link_patient_and_thread($post_id, $form_data) { // if a specific form if ($form_data['id'] == 12345 ) { toolset_connect_posts( 'patient-thread', $post_id, $_POST['patient'] ); } }
5. Create an Edit Thread Form and place it in an unassigned Layout.
6. Use the Forms API to programmatically modify the Form redirect for the New Thread Form, and point it to the Edit Thread Form URL:
add_filter('cred_success_redirect', 'custom_redirect_to_edit_thread',10,3); function custom_redirect_to_edit_thread($url, $post_id, $form_data) { if ($form_data['id']==12345) $redirect = get_the_permalink($post_id) . '?layout_id=67890'; return $redirect; return $url; }
7. Use the Forms API to add complex HTML to the new Thread post_content, including information from the Patient post:
add_action('cred_before_save_data', 'cred_generate_new_thread_content',10,1); function cred_generate_new_thread_content($form_data) { // if a specific form if ($form_data['id']==12345) { if (isset($_POST['patient'])) { // build the post_content using the patient's information $patient = $_POST['patient']; $dob = types_render_field("date-of-birth", array("style" => "text", "id" => $patient) ); $body = "<h2>Patient: " . get_the_title( $patient ) . "</h2>"; $body .= "<div>Patient's date of birth: " . $dob . "</div>"; $_POST['post_content'] = $body; } } }
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 14 replies, has 2 voices.
Last updated by 6 years, 2 months ago.
Assisted by: Christian Cox.