How to Use Smart Tags in Your WordPress Rewrite Rules

Introduction

Would you like to use your WPForms Smart Tags in your WordPress rewrite rules? You may have a custom post type that you’d like to pass through an email address captured from your WPForms submissions to display in your rewrite rules and in this tutorial, we’ll show you how to use a small PHP snippet you can easily achieve this.

A URL rewrite rule will take a standard URL and modify its appearance. It won’t change the location of the page, it just changes how the URL appears in your visitor’s browser window.

Setting up the rewrite rule

For the purpose of this documentation, we’ll assume you have already added the necessary code snippet to create your WordPress rewrite rule.

For further information on creating rewrite rules, please visit the WordPress.org reference material on this subject.

In our tutorial, we have a custom post type of portfolio where we use WPForms to allow members to upload photography images to their online portfolio through our form.

We want to create a dedicated page template in our theme that all of our photographers can use so that when visitors click our link, it will show them a page of only that photographers uploaded portfolio items. In order to achieve this, we need a rewrite rule to direct visitors to this page template.

add_rewrite_rule(
'portfolio/vendor/1/([a-zA-Z0-9]+)/?$',
'index.php?pagename=portfolio-vendor&email_address=$matches[1]',
'top' );

Setting up the form

Now it’s time to set up our form. Since, in this tutorial, we’re accepting uploads, we’re going to create a new post submission form using WPForms Post Submissions addon.

If you need assistance in creating this type of form, please check out this documentation.

We need to pass the Email Smart Tag inside a query string to build our rewrite rule. To do this, click on the Advanced tab of the Email form field and inside the Default Value, add the Smart Tag {query_var key="email-address"}.

add the Query Var Smart Tag for the email address

Adding the PHP code snippet

Now it’s time to add the code snippet to your site.

If you need help with where and how to add snippets to your site, please check out this tutorial.

/**
 * Using Smart Tags in WordPress rewrite rules
 *
 * @link https://wpforms.com/developers/how-to-use-smart-tags-in-your-wordpress-rewrite-rules/
 */

// Register custom query var
function register_query_var( $wp ) {
	parse_str( $wp->matched_query, $url );

	if ( ! isset( $url[ 'email_address' ] ) ) {
		return;
	}

	$wp->set_query_var( 'email_address', $url[ 'email_address' ] );
}

add_filter( 'parse_request', 'register_query_var', 9, 1 );

// Replace query var in WPForms smat tags
function my_process_smart_tags( $content ) {

	// Query string var smart tags.
	preg_match_all( "/\{query_var key=\"(.+?)\"\}/", $content, $query_vars );

	if ( ! empty( $query_vars[1] ) ) {
		foreach ( $query_vars[1] as $key => $query_var ) {
			$value   = get_query_var( $query_var ); // phpcs:ignore
			$content = str_replace( $query_vars[0][ $key ], strip_shortcodes( $value ), $content );
		}

	}

	return $content;

}
add_filter( 'wpforms_process_smart_tags', 'my_process_smart_tags', 9, 1 );

Now instead of your URL showing in the browser like this https://example-site.com/[email protected] it will now appear as https://example-site.com/portfolio/vendor/1/[email protected]/

And that’s it! You are now able to use a Smart Tag inside your WordPress rewrite rules. Would you like to create a custom Smart Tag? Take a look at our tutorial on How to Create a Custom Smart Tag.

Filter Reference: wpforms_process_smart_tags