How to Include Post Submissions Post URL in the Confirmation Message

Overview

Are you using the Post Submission addon and would like to include the post URL inside your confirmation message? This is easily done with a few steps using a PHP snippet and WPForms Smart Tags and we’ll walk you through each step.

Smart Tags are quick reference links that you can use inside the confirmation messages. There are a ton of pre-built in tags already created for you in WPForms and for more information on what’s already available for you, please see this documentation.

But did you know that you can also create your own Smart Tags to use in WPForms? In this tutorial, we’re going to create three new custom Smart Tags.

  • Submitted Post Title
  • Submitted Post Type ID Number
  • Submitted Post URL

These tags will be handy to use inside our confirmation message so that we can display a link to the post for the user who just submitted the form.

Creating your form

If you need help creating a form for the Post Submission addon, please review this tutorial.

For the purpose of this tutorial, we’ve already created our form with the default fields that come with the Blog Post Submission Form template.

Just create the post submission form with your required fields.

Setting the post status

For our tutorial, we’re setting the post to Pending Review status once the forms is submitted.

Set the status of the post to your choice. Just remember if the status is set to Draft or Pending Review, users won't see the post unless they are logged into your WordPress site.

Please remember that if the users submitting the posts are not logged into your WordPress site and you have the status of your posts (after submission) set to Draft or Pending Review, when they try to view the post URL from the confirmation message they will get a 404 page as you can only view draft posts while logged into WordPress.

Adding the post URL to the confirmation message

Once your form settings are complete, click the Confirmations tab.

Edit your confirmation message by clicking the Confirmations tab from the form builder Settings screen

Since we don’t just want to display the post URL but actually make it a clickable link, click the Text tab on the Editor window.

Click over to the text tab

We’re going to place our text and HTML for our message and post URL by typing the following:

<p>Thanks for the submission! 
If you'd like to see a preview of your post titled 
<strong>{submitted_cpt_title}</strong> for the post type ID of 
<strong>{submitted_cpt_id}</strong>, just 
<a href="{submitted_cpt_url}">click here</a>.</p>

<p>It will be reviewed shortly.</p>

Just add the confirmation message with the new Smart Tags and save the form.

We’re adding a message that is informing the visitor submitting the form the Post Type ID Number, the Post Title and the Post URL.

Once you’ve added your message, click Save on the form and you’re ready for the next step!

Adding the code snippet

Now it’s time to add our code snippet to your site that will pull this all together. If you need help in how to add code snippets to your site, please see this tutorial.

/**
 * Add Smart Tags to the Post Submission confirmation messages
 *
 * @link   https://wpforms.com/developers/how-to-include-post-submissions-post-url-in-the-confirmation-message
 *
 */

// This function sets up the names of the Smart Tags we will be using.
function wpf_dev_register_smarttag( $tags ) {

	// Key is the tag, value is the tag name.
	$tags['submitted_cpt_id']    = 'Submitted Post Type ID';
	$tags['submitted_cpt_url']   = 'Submitted Post Type URL';
	$tags['submitted_cpt_title'] = 'Submitted Post Type Title';

	return $tags;
}

add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );

//This function will process the Smart Tags so that we can 
//use them inside the confirmation message and email notifications
function wpf_dev_process_smarttag( $content, $tag ) {

	if ( empty( $_POST['wpforms']['entry_id'] ) ) {
		return $content;
	}

	/** @var \WPForms_Entry_Handler $entry */
	static $entry;

	if ( empty( $entry ) ) {
		$entry = wpforms()->entry->get( (int) $_POST['wpforms']['entry_id'], [ 'cap' => false ] );
	}

	if ( empty( $entry->post_id ) ) {
		return $content;
	}

	switch ( $tag ) {

		case 'submitted_cpt_id':
			$content = str_replace( '{submitted_cpt_id}', (int) $entry->post_id, $content );
			break;

		case 'submitted_cpt_url':
			$content = str_replace( '{submitted_cpt_url}', esc_url( get_permalink( (int) $entry->post_id ) ), $content );
			break;

		case 'submitted_cpt_title':
			$title   = get_post_field( 'post_title', $entry->post_id );
			$content = str_replace( '{submitted_cpt_title}', esc_html( $title ), $content );
			break;
	}

	return $content;
}

add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );

Inside the first function, we’re setting up the Smart Tags with names and definitions of what information they will hold such as the title, URL and the ID number of the post type.

The second function processes these Smart Tags and will assign them the title, permalink (URL) and what the post type ID number is which will allow us to use these Smart Tags in our confirmation messages.

Now whenever we use the {Submitted Post Type URL}, {Submitted Post Type Title} or {Submitted Post Type ID} Smart Tag, they will automatically be assigned those values after a successful form submission and your visitors will be able to easily click the click here link we’ve added to our confirmation message and be taken to the draft (or full published post) of their submission as well as see the other information such as the Post ID and Post Title.

And now the users can click the post URL link inside the confirmation message to see their submission.

And that’s all you need! Would you like to create more custom Smart Tags? Take a look at our tutorial on How to Create a Custom Smart Tag.

Filter References:

FAQ

Q: Can I make the post URL open in a new window/tab?

A: Absolutely! If you want the link to open in a new tab, just create your HTML link like this:

<p>Thanks for the submission! 
If you'd like to see a preview of your post titled 
<strong>{submitted_cpt_title}</strong> for the post type ID of 
<strong>{submitted_cpt_id}</strong>, 
just <a href="{submitted_cpt_url}" target="_blank">click here</a>.</p>

<p>It will be reviewed shortly.</p>

Q: Can I use this in the email notifications?

A: Currently this is not available to use inside the email notifications.