Skip Navigation

[Resolved] Prepopulate the parent selector when using a Repeatable Form Group

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by Luo Yang 5 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1137023

Tell us what you are trying to do?
I have a post type "bb" with a Post Form for front end users. At the end of the form the user can select to "Add a room" Repeatable Form Group form. The Add Room Form has the parent select dropdown. I want to avoid showing that to the user and have the parent be automatically filled and hidden.

Is there any documentation that you are following?
I've followed this: https://toolset.com/forums/topic/current-user-posts-in-child-posts/
I can't seem to get it to work.

Here is the pertinent part of the shortcode for the child post form - "bb" is the parent CPT:

<input type="hidden" id="parents_id" value="[get-parents]" />
[cred_field field='_wpcf_belongs_bb_id' value='' select_text='--- not set ---' class='form-control' output='bootstrap']
[cred_field field="@add-room-field.parent" class="form-control" output="bootstrap" select_text="--- not set ---"]
</div>
[cred_field field="form_submit" output="bootstrap" value="Submit" class="btn btn-primary btn-lg"]
[/credform]

Here is the JS code in that form:

jQuery('document').ready(function(){

var post_parents = jQuery('#parents_id').val();

var arr = post_parents.split(',');

jQuery("[name=_wpcf_belongs_bb_id] > option").each(function() {

var option_val = jQuery(this).val();

if( jQuery.inArray(option_val, arr) == -1 && option_val != -1 ){
jQuery(this).remove();
}
});

});

And here is the functions.php code I've used via the Code Snippets plugin:

function get_parents($atts) {
global $current_user;
get_currentuserinfo();
$author_query = array('post_type' => 'bb', 'posts_per_page' => '-1','author' => $current_user->ID,);
$author_posts = new WP_Query($author_query);
$parent_ids = "";
while($author_posts->have_posts()) : $author_posts->the_post();
$parent_ids .= get_the_ID() .",";
endwhile;

return $parent_ids;
}
add_shortcode('get-parents', 'get_parents');

I've also read this:
https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/#creating-forms-when-a-parent-post-is-preselected
This seems to require that the form be embed on the frontend parent post - that will not work for me.

What is the link to your site?
hidden link

Thanks for your help.
Wayne

#1137445
child-form-link.JPG

Dear Wayne,

The thread you mentioned above is outdated, I suggest you follow that document above to display a "add room" link in a single "bb" post:
https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/#creating-forms-when-a-parent-post-is-preselected

You don't need to embed the child "room" form in parent "bb" post, you can display just a form link, for example:
1) Create a wordpress page "Add new room", display the post form for creating "room" post
2) In a single "bb" post content, by clicking "Toolset Forms" button, click button "Create Child Post Link", in the following dialog window:
- option "Page that contains the form", choose the page "Add new room"(step 1)
- choose option "Set the parent according to the currently displayed content"
See screenshot child-form-link.JPG

More help:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_child_link_form

#1137543

Hi there,
Thanks for the quick response. I think I used the wrong terminology in describing my need. I'll try again - what I need is that after having filled in the bb post parent form that the user, upon submission, be directed to the frontend "add room" cred form. I don't want the user to have to go to the front end post. After submitting the first room, I currently have the "add room" form set up such that the user can then select an "add another room" button or a "check out" button. If they select "add room" they are then directed to the same "add room" form. The user never should have to see their published front end post until both the parent post form and all child post forms are submitted.
I hope this better describes my need.
Thanks again for your help.
Wayne

#1138034

Thanks for the clarification.

In your case, I suggest you try these:
1) Create a wordpress page "Add new room"(for example page ID is 1234), display the post form for creating "room" post

2) After user submit the post form for creating parent "bb" post, use filter hook "cred_success_redirect" to trigger a PHP function, in this function pass a URL parameter "parent_[parent-type-slug]_id" to page "Add new room" page, for example:

add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data)
{
    if ($form_data['id']==123){ // Post form ID
        $url = get_permalink(1234); // target page ID
        $url .= '?parent_bb_id=$post_id';
    }
    return $url;
}

Please replace "bb" with your custom post type "bb" slug.

Then the user will be redirected to the page "Add new room" with URL parameter "parent_bb_id", and the parent selector of child form will be pre-selected.

Similar as above, after user submits the child form, you can check if the "add another room" is enabled, then redirect him to the same page, with same URL parameter "parent_bb_id".

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect
hidden link

#1138361

Hello again,
Thanks for your help so far.
I'm understand everything (I think) up to the point where the user submits the child form.
I'm not sure though about the the following instruction:
"Similar as above, after user submits the child form, you can check if the "add another room" is enabled, then redirect him to the same page, with same URL parameter "parent_bb_id"."

On the child form (Add Room form), what should the settings for "What to show after submitting the form"?
And, how do I "Check if the 'Add another room' is enabled?

Cheers, Wayne

#1138832

In the form content for creating child "Room" post, you can add a generic checkbox field "add another room", for example:

[cred_generic_field type='checkbox' field='add-another-room']
{
"required":0,
"default":"1",
"label":"add another room"
}
[/cred_generic_field]

https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field
when your user submit this form, and enable the checkbox field "add another room", you can use same filter hook "cred_success_redirect" to trigger a PHP function, in this function, check if the checkbox field is enabled, then redirect to the same page for creating new room post.

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect
hidden link

#1139176

Hello again

In your reply and code example above (#1138034), you use the following line:
if ($form_data['id']==123){ // Post form ID
I can't seem to get it to work and have tried both the post form id for the parent and the post form id for the 'add room' post form.

Where that leaves me is that I can't even add the first room with the related parent post prepulated in the selector once I reach the "Add room" page.

I'm pretty sure that you meant for me to use the parent post but in either case, here is the url that user is directed to in case this helps you:
hidden link

Any ideas what I'm doing wrong?

Thanks again,
Wayne

#1140092

As you can see in the URL you mentioned above, the form ID is 206719, you will need to replace 123 with 206719.

And please check these:
1) In case it is a compatibility problem, please deactivate other plugins, and switch to wordpress default theme 2017, copy above codes into current theme file functions.php, and test again
2) If the problem still persists, since it is a custom PHP codes problem, please provide your website credentials + FTP access in below private message box, also point out the problem page URL and form URL, where I can edit your PHP codes, I need a live website to test and debug

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