How to Use Smart Tags in Your WordPress Rewrite Rules

Overview

Would you like to use your WPForms Smart Tags inside 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.

Setup

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.

1) Setting up the rewrite rule

In our tutorial, have a custom post type of portfolio where we use WPForms to allow members to upload photography images to their online portfolio through a form. The idea behind the custom rewrite rule is that there is a dedicated page template when clicked, will load that photographers’ dedicated page that shows only images uploaded by them. So we needed 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' );

2) Setting up the form

Now it’s time to set up our form. We need to pass the Email Smart Tag inside a query string to build our rewrite rule. To do this, you’ll need to set up the Email form field to accept the query string.

set up the query string in order to use smart tags in rewrite rules

3) Adding the PHP code snippet

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

/**
 * 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' );

// 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 );

Now instead of your URL showing in the browser like this https://example-site.com/?pagename=portfolio-vendor&email_address=sullie@wpforms.com it will now appear as https://example-site.com/portfolio/vendor/1/sullie@wpforms.com/

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