USP Pro makes it easy to set custom redirects to any URL after the form is submitted. This works out of the box, but success and error messages need to be added manually. This quick tutorial explains an easy way to implement response messages for custom redirects.

UPDATE: USP Pro Custom Redirects extension now available! The new add-on automatically displays success and error messages when custom redirects are enabled. So you can either implement your own solution as described in the following tutorial, or grab a copy of the extension and do it automatically.

Step 1: Implementing custom redirects

There are two ways to set custom redirects, globally and locally (per form):

Global redirects

Global redirects are set in the plugin settings under Advanced ▸ Form Configuration ▸ “Redirect URL for Success” and “Redirect URL for Failure”. You can learn more about these settings in this tutorial on Custom Redirects for USP Pro. After specifying a value for either of these settings, it will be used for all forms unless a local value is set.

Local redirects

Setting a custom redirect for any form, add the [usp_redirect] shortcode, like so:

[usp_redirect url="http://example.com/redirect-page/"]

With this in place, the visitor will be redirected to http://example.com/redirect-page/ after submission. Learn more about local redirects in the tutorial Custom Redirects for USP Pro.

Note that if you are not using custom redirects, you can set your own success and error messages via the plugin settings, under the “Advanced” and “More” tabs. Otherwise, they need to implemented manually. Continue reading to learn how it’s done.

Step 2: Understanding redirect parameters

Once you have a custom redirect in place, after submitting the form, the visitor will be redirected to the specified URL with one of the following parameters included in the query string:

  • usp_success – successful form submission
  • usp_error_n – failed form submission (where n = the error number)

These parameters will be included in the URL, so it will look something like this:

  • http://example.com/redirect-page/?usp_success=1
  • http://example.com/redirect-page/?usp_error_1

Equipped with this information, we are ready to implement success and error messages for our custom redirect.

Step 3: Implementing custom success and error messages

To add a message that is displayed when the form is submitted successfully, add the following code to the desired location in your theme template file:

if (isset($_GET['usp_success'])) echo "Success! Post submitted..";

Likewise, to display a message when the form submission is not successful, we can add the following to the associated theme file:

if (isset($_GET['usp_error_1'])) echo "Ooops! Error 1";

With USP Pro, there are numerous errors that can be customized, for example:

<?php // custom error messages

if (isset($_GET['usp_error_1'])) echo "Ooops! Error 1";
if (isset($_GET['usp_error_2'])) echo "Ooops! Error 2";
if (isset($_GET['usp_error_3'])) echo "Ooops! Error 3";

// ...

?>

That code would be included in whichever theme template is used to display your custom error page. Here is a complete list of USP Pro Error Definitions. Probably don’t need to define a message for every error, just the ones that are relevant to the form in question.

Integrating via functions.php

Instead of including the above code in “whichever theme template is used to display your custom error page” (as suggested previously), you can include via your theme’s functions.php file. Here is an example:

function usp_custom_error_messages() {
	if (isset($_GET['usp_error_1'])) echo "Ooops! Error 1..";
	if (isset($_GET['usp_error_2'])) echo "Ooops! Error 2..";
	if (isset($_GET['usp_error_3'])) echo "Ooops! Error 3..";
}
add_action('init', 'usp_custom_error_messages');

The trick here is hooking our custom function into WP’s init hook, which enables us to check the $_GET variable for the specified parameters. Here is another example showing how to hook a custom success function:

function usp_custom_success_message() {
	if (isset($_GET['usp_success'])) echo "Success! Post submitted..";
}
add_action('init', 'usp_custom_success_message');

Going the init route via functions.php (or custom plugin) is a good way to keep your other theme template files neat and organized. Also keep in mind that for success messages, the $_GET variable includes the Post ID, so you can grab that and do some amazing things.

Of course, this is just a basic guide to get you started. Using the magic of PHP and the WP API, it’s possible to do very advanced things with success and error messages for custom form redirects. Note: eventually this functionality will be built in to the plugin, but for now it must be implemented manually.

Related