Skip Navigation

[Resolved] Redirect change form after input to a spesifique archive content template?

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to redirect Users from Forms to archive pages.

Solution: Use the cred_success_redirect API to redirect to other URLs:

add_filter('cred_success_redirect', 'custom_archive_redirect_for_forms', 10, 3);
function custom_archive_redirect_for_forms( $url, $post_id, $form_data )
{
  // update this hash to include one line for each form ID and its respective redirect destination URL
  $redirect_for_forms = array(
    'f123' => 'https://yoursite.com/some/archive/url',
    'f234' => 'https://yoursite.com/some/other/url',
    'f345' => 'https://yoursite.com/yet/another/url'
  );
  // -----     You should not edit below this line ------
 
  // test if the array key exists first, then return the proper URL if the form ID is found in the hash
  if( array_key_exists( 'f'.$form_data['id'], $redirect_for_forms ) ) {
    return $redirect_for_forms['f' . $form_data['id']];
  }
 
  // otherwise, just return the original destination url
  return $url;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

This support ticket is created 4 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.

Our next available supporter will start replying to tickets in about 2.50 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by samuelH 4 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1441065

Is it possible to redirect a "change form" after submission to a spesifique archive content template? Or may I bypass so the archive gets presented on a page?

#1441467
Screen Shot 2020-01-05 at 5.29.44 PM.png

Hi, if you would like to redirect to an archive URL after submitting an Edit Post or Edit User Form, I have a custom code snippet you can use to set up that custom redirect. The cred_success_redirect API is designed to help you implement this kind of customization. First, you must set the Form to redirect to some custom Page or post. It doesn't matter which one you choose, but one must be selected in order to trigger the custom redirect. Then, you need to find the Form's numeric ID. You can find that ID in the wp-admin Forms dashboard, next to the Form name in the list of Forms. See the screenshot attached here for an example.

Next, you must add a custom code snippet. If your site uses a child theme, you can paste the code in your child theme's functions.php file before the closing PHP tag (if one exists). Or, you can go to Toolset > Settings > Custom Code, and create a new custom code snippet with "Run mode" set to "Run always", and "Run context" set to "Front-end" and "AJAX calls". Paste this code at the end of the snippet:

add_filter('cred_success_redirect_999', 'custom_redirect_for_form_with_id_999', 10, 3);
function custom_redirect_for_form_with_id_999( $url, $post_id, $form_data )
{   
    $newurl = '<em><u>hidden link</u></em>';
    return $newurl;
}

Replace 999 with the numeric ID of this Form - all 3 places here. Note that there are 3 instances where you must replace 999 in this sample code. Then replace hidden link; with the full URL you would like to use for the redirect. Save the snippet (and activate it if necessary), then your Form should redirect as expected.

Let me know if you have trouble with this and I can offer some more guidance. We have documentation for this API available here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

#1442103

Hi Christian,
Great - that worked splendid!

I presume I can duplicate and adjust this to apply the same to other forms? Or maybe it is possible to write it all in one function?

This will apply to about 50 forms, and then 50 redirect posts, and ending at 50 different archive pages.

#1443157

Sure, you could duplicate and adjust as needed or you could refactor the code to use a single function for all the forms. This approach implements a data hash created from Form IDs paired with their respective destination archive URLs. Here is an example:

add_filter('cred_success_redirect', 'custom_archive_redirect_for_forms', 10, 3);
function custom_archive_redirect_for_forms( $url, $post_id, $form_data )
{
  // update this hash to include one line for each form ID and its respective redirect destination URL
  $redirect_for_forms = array(
    'f123' => '<em><u>hidden link</u></em>',
    'f234' => '<em><u>hidden link</u></em>',
    'f345' => '<em><u>hidden link</u></em>'
  );
  // -----     You should not edit below this line ------

  // test if the array key exists first, then return the proper URL if the form ID is found in the hash
  if( array_key_exists( 'f'.$form_data['id'], $redirect_for_forms ) ) {
    return $redirect_for_forms['f' . $form_data['id']];
  }

  // otherwise, just return the original destination url
  return $url;
}

The $redirect_for_forms array must be updated to include a line for each Form, with a comma separating each line. The first part like f123 should be replaced with the letter "f" plus the numeric ID of each Form. The URL should be replaced with the corresponding redirect destination URL.

#1448045

My issue is resolved now. Thank you!