Skip Navigation

[Resolved] How to load a specific custom post into a post form after submitting a form

This support ticket is created 2 years, 11 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 – 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 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 3 voices.

Last updated by Shane 2 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#2069129

Tell us what you are trying to do?

- I have a custom user role called 'Client'
- I have a page which contains a 'user' form which allows a website admin to create a 'Client' user
- I also have a parallel custom post type of 'Client' which allows me to create relationships between Client users and other custom post types, using 'relationships'.
- Currently after a Client 'User' is setup, I have a script in my functions.php which runs after the user form is submitted, and creates the parallel Client 'Post' - THIS IS WORKING FINE
- After the Client User form is submitted (and the script above runs), what I then want to do is to load a page which contains the POST form for the 'Client' custom post type (this is fine), but I want this form to then be pre-populated with the data from the Client 'Post' which was just created (hopefully this makes sense!)
** The reason I want to load this parallel Client Post, is becuase the 'Post' version of client will also contain some extra custom fields, which the 'User' version of the Client does NOT contain (basically the User contains an email address, a first name and a last name, but I want the Post to contain other information about the client, which will need to be filled in by the website admin who is setting up the client, so in essence, they need to fill out 2 forms to set up 1 client, 1 form is the User, and 1 form is the Post, but I want the email address from the User to be used to load up the corresponding Post version of the same user, into the Post Form)

I thought that maybe this would be achievable by using the 'After visitors submit this form:' setting on my Client User Form, but it seems that it is not. I can load the page which contains the Client Post Form (the slug for this page is 'add-client-details'), so in the 'After visitors submit this form' setting I select 'Page' and then I select my 'Add Client Details' page, but there is no option for me to add anything else to the URL to pre-populate any of the fields in the Client Post Form which loads on that page.
However I notice that after I fill out my Client User Form, the URL it redirects to is as follows:

my domain/add-client-details/?cred_referrer_form_id=146 (146 is of course the ID of my Client User Form)

So - my question is, how do I automate it so that the data that gets loaded into my Client Post Form (on my 'add-client-details' page) contains the data from the Client Post which was just created? Currently the Client Post Form just loads in data from the 1st Client Post (ie the first post alphabetically).

Thanks very much in advance for your help,

Keith

Is there any documentation that you are following?
I have tried searching through your forums, but I couldn't find anything which solved this particular issue

Is there a similar example that we can see?
I can of course supply you with a link / logins etc, if you can able a private box on this thread for me to share these details

What is the link to your site?
Please see answer to previous question

#2069527
Screen Shot 2021-05-26 at 4.58.03 PM.png

Hi, you're on the right track with redirection and URL parameters. but you'll need some additional customization here because the built-in options for the redirect destination URL are not flexible enough to support parameters. For the benefit of anyone who reads this ticket and may not be aware, it is possible to use URL parameters to predefine field values in a Form. So if you include your second Form in a post or page at a URL like:
https://yoursite.com/page-with-form-two/
...you can predefine field values by redirecting the User from Form 1 to a URL like:
https://yoursite.com/page-with-form-two/?p_one=123&p_two=abc&p_three=some%20encoded%20text
Notice the pattern is "?" before the first URL parameter name, then "&" before each subsequent URL parameter name. We offer the cred_success_redirect API with which you can customize the redirect URL programmatically. In the Form settings, you must choose any of the options that triggers a redirect, e.g. "Go to..." It doesn't matter which page or post you choose to redirect to, but you must choose one. The custom code will override your selection automatically.

Check out a similar scenario in another ticket, with example code snippets:
https://toolset.com/forums/topic/add-parameter-with-filled-field-data-to-redirected-url-after-form-sent/
That example shows a cred_field shortcode with the URL parameter name set in a urlparam attribute. If you're using the drag-and-drop form builder instead of the shortcode builder, you can set the URL parameter name in the twirl-down options panel for each field (screenshot attached).

Let me know if you have questions about this, or if I've misunderstood things.

#2071237
Client User Setup Process.png

Hi Christian, I hope you're well. Thank you as always for your excellent help. Unfortunately I think that I haven't explained my issue quite correctly, so my apologies for that.
Although your answer helps me understand the function that I needed to create (for after the initial User Form is submitted - using the 'cred_save_data' action), I'm not getting the desired outcome:

I need to be able to dynamically load up the details of the Client 'Post' (that gets auto-generated after I submit the User Form), into the Post Form on my redirection page (so the Post Form is a form to update 'existing' posts, not add new posts). I think the code you have given me would allow me to pre-populate the fields for a 'new' post. However, in this case, the post is already setup, so I need the details for that post to load into the Post Form on my redirection page (that Post Form is to edit existing posts, not to create new ones).

I realise that I might not be explaining myself very well - so I have drawn a flow diagram, which hopefully makes it easier to understand - please see my attached image which explains things better (I hope!) Basically my question is shown twice, once in red and once in blue, but they are the same question (I have just tried to explain it in 2 different ways to try and explain better!).

Just to confirm, step 1, 2 and 3 in the diagram are all working fine, it is just Step 4 which I am trying to solve in this thread.

So I just need to be able to get the Post ID of the automatically generated Client Post (the post in the orange box), and then to be able to add that to the URL of the redirection (but again if you see my flow diagram I think this will explain it better). After looking around the forum I have stumbled on some code which I have tried to adapt (and I also found the 'wpv-search-term param' shortcode bit in some other threads), and I added this code to my functions.php - please see my code below:

add_filter('cred_success_redirect', 'after_client_setup_redirect',10,3);
function after_client_setup_redirect($url, $post_id, $form_data)
{
if ($form_data['id']==146) {
$redirect = $url . "?client_post_id" . $post_id;
return $redirect;
}
return $url;
}

But unfortunately this code does not work, it is pasting an incorrect post ID into the URL (it was a stab in the dark from me haha).

Please let me know if you need any more information from me.

And once again, thank you SO MUCH for your excellent support 🙂

Keith

#2073801

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

Thank you for the clarity I picked this ticket from Christian's queue while he was on vacation.

Reading through it would appear that you want to edit the existing Post that the user would've already created. This means that when you go to the page with the form you want to preload all the user's existing data that was placed in the post and allow them to edit that post and fill out the additional data that would be needed correct?

As you say you are using the hook below to pass the ID to the edit form

add_filter('cred_success_redirect', 'after_client_setup_redirect',10,3);
function after_client_setup_redirect($url, $post_id, $form_data)
{
if ($form_data['id']==146) {
$redirect = $url . "?client_post_id" . $post_id;
return $redirect;
}
return $url;
}

However I see what you are passing is not what would be the ID of the client's post but the ID of the form that was previously submitted.

What you should instead do is to get the post that the user created under the post type.

This can be done by doing the following

add_filter('cred_success_redirect', 'after_client_setup_redirect',10,3);
function after_client_setup_redirect($url, $post_id, $form_data){
$current_user_id = get_current_user_id();

if ($form_data['id']==524) {
	$args = array(
    	'author'=>  $current_user->ID, // I could also use $user_ID, right?
		'post_type' => 'Post Type Slug',
  		'numberposts'=> 1
 	);
	// get his posts 
	$current_user_posts = get_posts( $args );
	$redirect = $url . "?client_post_id" . $current_user_posts[0]->ID;
	return $redirect;
}
return $url;
}

Replace "Post Type Slug" with the correct slug of the post type that you want to pull the ID for.

Please let me know if this helps.
Thanks,
Shane

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