Skip Navigation

[Resolved] Entering repeate field/child posts

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 4 replies, has 2 voices.

Last updated by Bob 1 year, 2 months ago.

Assisted by: Nigel.

Author
Posts
#2556545

Bob

I want to create a form to populate a work list, simply it'll create a job, (one custom post) which will then have multiple line items (separate post type with repeatable fields).

Example:
Job1 will have line items such as "Do This" and "Do That"

Reading this https://toolset.com/course-lesson/front-end-forms-for-repeatable-field-groups/ suggests I will have to create the job first which is fine but I will then have to create each line item one form submit at a time is there anyway around this?

If it requires additional coding that's OK but can you give me an outline of how to approach it?

#2556621

Bob

I have a simpler explanation of what I want to do - On the Gym demo site would it be possible to include the opening hours on the Submit Gym form?

#2556695

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screenshot 2023-02-17 at 15.49.34.png

Hi Bob

The Gym site doesn't include a way to add opening hours from the front end, for a reason, namely that the workflow is somewhat awkward, and if you want a reasonable workflow it requires a little bit of custom code using the Forms API (the Reference sites do not include features that depend on custom code).

It is not possible to include repeatable groups of fields within a form to publish or edit posts that the fields belong to. As currently configured Forms can only publish (or edit) a single post at a time, and repeatable groups of fields are implemented using post relationships (each group of fields belong to a post, which is a child post of the post the fields "belong" to).

Hence the workflow for adding repeatable groups of fields from the front end is very much like that for adding child posts to an existing parent, and requires separate forms to publish the parent and to publish the child repeatable groups of fields.

I have set up a demonstration using the Gym reference site of what I consider the most frictionless workflow for adding repeatable groups of fields (in this case, adding opening hours to a gym).

In the screenshot you can see how a gym post appears on the front end, using the updated content template.

There is an existing link to edit the gym (visible to the author of the gym post), and I have added a link to add Opening Hours below where the existing opening hours are shown.

If you click that link it takes you to a page with a form to add opening hours (one opening hour post, e.g. for Thursday). When you complete and submit that form then it returns to the display of the gym post as per the screenshot, from which you can then click the link to add more Opening Hours as required.

To see how this was implemented, use this link to log in to the site: https://shiny-tabla.sandbox.otgs.work/?auto=rQo1wVIyzyJJ5kzlEBOzn1vLskIGpwgN

At Toolset > Forms you'll note a new form, Add Opening Hours.

If you edit it, as well as the fields themselves, note the settings, to publish a post of the post type pertaining to the RFG Opening Hours. And note the setting to redirect to the homepage after the form is submitted. We will override this with code, to redirect back to the parent gym, but the code will only be triggered if the form is trying to redirect somewhere.

Lastly, note that the form includes a title field which is required, and so I have provided a default value (the title of the parent Gym), although I would hide the field using CSS as the user does not need to see it. Similarly, hide the select field for the parent Gym, which will be populated with the ID of the gym that links to the form, but which users do not need to see.

Now, edit the Content Template for displaying Gym posts, and note how I added a Fields and Text block below the View that displays opening hours, to generate the link to the form to add Opening Hour posts. You can see the resulting cred_child_link_form shortcode (which I generated by clicking the Forms icon in the Fields and Text block).

Lastly, if you go to Toolset > Settings > Custom Code you will see a code snippet that I added which uses the cred_success_redirect hook to override where the form redirects to upon submission (using the ID of the parent gym available from the $_REQUEST or $_POST object that contains details of the form field entries).

You could use similar techniques, editing the output of the View that displays the Opening Hour posts to add edit and delete links for each row of the Opening Hours table, bearing in mind that you would handle these in the same way as linking to forms to edit or delete child posts.

By all means test the form submission to see the workflow, but please don't change the settings, I have another user with a similar question who will be looking at the same site.

For reference, here is the code used for the redirect:

/**
 * Redirect RFG form back to its parent post upon submission
 */
add_filter('cred_success_redirect', 'ts_redirect_rfg_form',10,3);
function ts_redirect_rfg_form($url, $post_id, $form_data)
{
    if ( $form_data['id'] == 14458 && isset( $_REQUEST['@opening-hours_parent'] ) )
    {
        $url = get_permalink( $_REQUEST['@opening-hours_parent'] );
    }
   
    return $url;
}
#2560201

Bob

I'll close this for now, I haven't had a chance to explore your very full answer, thanks for that.
Hopefully that'll do the job or I'll come back but thanks for this regardless.

#2560203

Bob

Thanks

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.