Problem:
The customer was working on a Custom Post Type ("Object") that represents artworks on their museum platform. They created specific content templates for different museums to cater to their unique display requirements. However, there was no option in the Custom Post Type Forms for the museum staff to assign the appropriate content template when creating or editing an artwork. The customer wanted to eliminate the need for staff to navigate the wp-admin editing page, which could lead to confusion and errors.
Solution:
We suggested utilizing a generic field in the form to enable the selection of a content template. The customer successfully implemented a dropdown select field, but encountered an issue where the selected option did not display when returning to the form. To resolve this, we provided a custom PHP function that retrieves the current template assigned to the post. The code snippet for the function is as follows:
add_action('cred_save_data', 'my_save_data_action', 10, 2); function my_save_data_action($post_id, $form_data) { // Array of form IDs for which the action should be triggered $form_ids = array(21166, 18806); // Check if the current form ID is in the list if (in_array($form_data['id'], $form_ids)) { if (isset($_POST['select-template'])) { update_post_meta($post_id, '_views_template', sanitize_text_field($_POST['select-template'])); } } }
After further testing, we recommended that for museum editors, the solution could be simplified by hardcoding the content template ID to ensure the correct template is always assigned. This change allowed for the expected functionality when creating or editing artworks.
Relevant Documentation:
https://toolset.com/course-lesson/conditional-display-for-form-inputs/
https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_generic_field
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 |
---|---|---|---|---|---|---|
- | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | - |
- | 13:00 – 18:00 | 13:00 – 18:00 | 13:00 – 18:00 | 13:00 – 18:00 | 13:00 – 18:00 | - |
Supporter timezone: America/Sao_Paulo (GMT-03:00)
This topic contains 14 replies, has 2 voices.
Last updated by 3 months ago.
Assisted by: Mateus Getulio.