Skip Navigation

[Resolved] REDIRECTION PROBLEM FOR CPT WITH RGF

This support ticket is created 5 years, 3 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
- 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 10 replies, has 2 voices.

Last updated by Silvia 5 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#1184136

I am building a website that tracks all activities in my department. One of this activities is sending memos to the companies we assist, to let them know of new laws regarding their activity, events that are organised to promote them and so on. These memos often consist of one of more files in pdf or doc format, so I build a cpt with a RFG that contain 1) the actual file 2) a name or description for the file.

I created then 2 forms, one that adds the memo itself (date, description, recipient and subject) and one that adds RFG documents to the memo.

I followed the instructions in this post https://toolset.com/forums/topic/workflow-for-frontend-editing-of-repeatable-field-groups/ in order to be able to redirect the user automatically to the parent post once they have added files to a specific memo.
Now it works ok for the RFG when I insert a file, it actually redirects me to the parent memo and shows all the detail correctly… but for some reasons, it affects the Memo cpt as well. Even tho I instructed FORMS to show the post upon submission, after editing the functions.php (to add the custom cred_success_redirect function provided in the solution) each time I insert a memo it redirects me to a blank form to add another memo, instead of showing the one I just entered.
I was wondering if you could help me find out why.

Also, I have another CPT that has a RFG, and I was thinking of using the same function, only changing its name and the parameters to point to the right CPT. Can I do that?

#1184155

Nigel
Supporter

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

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

Let me just create a test with a similar set up to see if I can reproduce the problem you describe and to see what is required to get it working.

I'll get back to you.

#1184201

Nigel
Supporter

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

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

OK, I tested the following set-up with the code shared below.

I have a main post type, which has an RFG. (If you didn't know already, under the hood these are set up the same as a parent - child post relationship.)

I have a form to publish the main post type, set to redirect to the post just published, inserted on its own page.

I have a form to publish an RFG, set to redirect to some page (it doesn't matter which), inserted on its own page.

I have a content template to display the main post type, which includes a link to add RFGs using this second form (using the cred_child_link_form shortcode).

I added the following code as a code snippet at Toolset > Settings > Custom Code:

/**
 * Redirect to parent post when submitting RFG post
 */
function tssupp_redirect_rfg($url, $post_id, $form_data) {

	if (in_array($form_data['id'], array(119))) { // Edit for form id (optionally add more ids to the array, e.g. array( 119, 123 )

		if (isset($_REQUEST['@items_parent'])) {

			$url = get_permalink($_REQUEST['@items_parent']);
		}
	}

	return $url;
}
add_filter('cred_success_redirect', 'tssupp_redirect_rfg', 10, 3);

So I now have the following workflow:

1. I visit the page with the form for the main post type and submit a new post
2. the page redirects to show the newly published post
3. the template to display this post includes a link to add an RFG
4. clicking that link takes me to the page with the RFG form, which has the parent post pre-selected
5. when I submit the form the page redirects to display the parent post, where I can follow the link to add more RFG instances

You can modify the above code so that it affects multiple RFG forms.

#1184243

Thank you for your quick response, Nigel!
I am relatively new at handling code so I do have a couple questions for you:
a) What form Id do I have to consider when editing the parameter "array(119)"? The parent cpt form or the RFG form?
b) In order to make it affect other forms, how do I add other form ID's?

Thank you again!

#1184246

Nigel
Supporter

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

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

It is the child (RFG) forms we are trying to customise the redirection for (to have them redirect to display the parent post they belong to).

So if you have several post types with RFGs that you want to set up something similar for, you need to make a note of each of the RFG form IDs.

Then edit the one line of the above code, like so:

    if (in_array($form_data['id'], array(119, 123, 135))) { 

You can add as many RFG form IDs to that array as you need.

#1184248

OK, I tested it but I got this workflow instead:

Steps 1 thru 4 ok.
Step 5. when I submit the form the page redirects to display the parent post, where I can follow the link to add more RFG instances -> instead of taking me back to the parent CPT it sent me to the homepage and the url has "?cred_referrer_form_id=1088" added to it. 1088 is the RFG Form Id that I tested.

#1184279

Nigel
Supporter

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

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

OK, I'll need to take a look at your site, I might need to edit the code and add some debug messages.

I have the site credentials, so I'll get back to you.

#1184975

Nigel
Supporter

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

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

Sorry Silvia

I didn't mention that the sample code I provided is based on the slug of the RFG from my test site, it needs changing to match the slug of the RFG on your site—which I don't know, in your description of the problem you didn't mention it.

I looked at your site but the post types are in Italian and you were giving me (different) descriptions to describe the issue in English, so I'm not sure what the post type is to see what the correct slug of the RFG would be, so you will need to edit it yourself.

In my code example you need to edit both instances of @items_parent and change 'items' to the slug of your RFG.

So where you apply this to multiple forms, that's okay as long as the slug of the RFG is the same for each form. If not you'll need to add some additional logic to test for different slugs depending on which form is being handled.

#1184985

Hi Nigel, thanks for your answer. I am sorry for the language barrier problem!
The slug of the RFG is "documenti-circcom" and as I edited the snippet as follows

/**
 * Redirect to parent post when submitting RFG post
 */
function tssupp_redirect_rfg($url, $post_id, $form_data) {
 
    if (in_array($form_data['id'], array(1088))) { // Edit for form id (optionally add more ids to the array, e.g. array( 119, 123 )
 
        if (isset($_REQUEST['@documenti-circcom_parent'])) {
 
            $url = get_permalink($_REQUEST['@documenti-circcom_parent']);
        }
    }
 
    return $url;
}
add_filter('cred_success_redirect', 'tssupp_redirect_rfg', 10, 3);

I got the following error:
[2019-01-16 10:41:44, ajax] Output inatteso del frammento di codice: /** * Redirect to parent post when submitting RFG post */ function tssupp_redirect_rfg($url, $post_id, $form_data) { if (in_array($form_data['id'], array(1088))) { // Edit for form id (optionally add more ids to the array, e.g. array( 119, 123 ) if (isset($_REQUEST['@items_parent'])) { $url = get_permalink($_REQUEST['@items_parent']); } } return $url; } add_filter('cred_success_redirect', 'tssupp_redirect_rfg', 10, 3);

I only changed @item_ with @documenti-circcom and changed 119 to 1088 which is the RFG form id. What am I doing wrong?

#1184992

Nigel
Supporter

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

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

When you add code snippets at Toolset > Settings > Custom Code they have an opening php tag, and in copying and pasting the code it seems like you lost it. I re-inserted it.

Do you want to test the form again?

#1184996

Thank you Nigel, works perfectly now!
As I said I am just beginning to handle code, and I have another CPT that has 2 RFGs ... so instead of trying to create a nested if to test different form IDs (and probably make a mess!) I guess I will achieve the same result if I copy and paste the function you provided, change its name and create a snippet for each RFG I have, right? Thank you for your help!!!

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