How to Create More User Smart Tags

Introduction

Would you like to create custom user Smart Tags to use in your WPForms that will pull information from the WordPress User Profile?

A Smart Tag is a bit of code that you can use to insert relevant information to your form notification settings or as field default values. In this tutorial, we’re going to create two new Smart Tags to be used by pulling information from the WordPress user profile for their Nickname and Username.

By default, there are already some user-related Smart Tags available with WPForms such as User ID, Display Name, User Meta etc.

The User Meta Smart Tag can be used as a blank coverage for any custom fields that may have been added to the WordPress User Profiles such as social links or any custom fields added to the WordPress User Profile.

Creating the new user Smart Tags

First, we’ll need to create these two new Smart Tags. To do so, we’ll add this code snippet to our site.

If you need help in adding code snippets to your site, please review this tutorial.

/**
 * Create a WPForms Smart Tag to pull Nickname and Username fields from the WordPress profile.
 *
 * @link   https://wpforms.com/developers/how-to-create-more-user-smart-tags/
 */
function wpf_dev_register_smarttag( $tags ) {

	// Key is the tag, item is the tag name.
	$tags['wp_nickname'] = 'WP Profile Nickname';
	$tags['wp_username'] = 'WP Profile Username';
	
	return $tags;
}

add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );


/**
 * Process the Smart Tag.
 *
 * @link   https://wpforms.com/developers/how-to-create-more-user-smart-tags/
 */

function wpf_dev_process_smarttag( $content, $tag ) {

	$userID = get_current_user_id();

	// Only run if it is our desired tag.
	if ( 'wp_nickname' === $tag ) {

		$wp_nickname = get_the_author_meta( 'nickname', $userID );
		// Replace the tag with the nickname pulled from the user's WordPress profile.
		$content = str_replace( '{wp_nickname}', $wp_nickname, $content );

	}

	if ( 'wp_username' === $tag ) {

		$wp_username = get_the_author_meta( 'user_login', $userID );
		// Replace the tag with the username pulled from the user's WordPress profile.
		$content = str_replace( '{wp_username}', $wp_username, $content );

	}

	return $content;
}

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

Now let’s take a closer look at each of these functions.

Creating the Smart Tags

Before we can define and use the Smart Tags, we first need to create them.

With the wpf_dev_register_smarttag function, we’re creating two new Smart Tags. The WP Profile Nickname and WP Profile Username.

First use a function to create the smart tag for the username and user nickname

This will allow us to use these Smart Tags inside the form builder when we build our form later.

Defining the Smart Tags

Now that we’ve created our Smart Tags, we need to define what they are. With the wpf_dev_process_smarttag, we’re telling the two Smart Tags to go to the user’s WordPress Profile and pull find the value that is stored in the Nickname and Username fields of the profile so when we use these Smart Tags, we can call on these values inside the form builder.

with the second function in the snippet we're defining what each of those smart tags are and where to find that information

Creating the form

Now it’s time to create our form so that we can use our new Smart Tags. If you need help in creating a form, please see this article.

Now you'll see your custom user smart tags

You can find more information on creating other Smart Tags by viewing the article on How to Create A Custom Smart Tag.

And that’s all you need in order to create more user Smart Tags with WPForms. Would you like to use those Smart Tags inside an HTML / Code Block? In our tutorial How to Process Smart Tags in HTML Fields, we’ll show you how to use PHP to allow Smart Tags in this type of form field.

Filter References:

FAQ

Q: Can I get the URL as well?

A: Absolutely! Here is an example of the code to get the URL from the user profile.

/**
 * Create a WPForms Smart Tag to pull User URL from the WordPress profile.
 *
 * @link   https://wpforms.com/developers/how-to-create-more-user-smart-tags/
 */

function wpf_dev_add_user_url( $content, $tag ) {

	if( $tag === 'user_url' ) {
		if ( is_user_logged_in() ) {
			$user = wp_get_current_user();
			$name = sanitize_text_field( $user->user_url );
		} else {
			$name = '';
		}

		$content = str_replace( '{' . $tag . '}', $name, $content );
	}

	return $content;
}

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

Q: How can I create a Smart Tag for the user role?

A: You can use this snippet to create a Smart Tag to display the user’s role as well.

/**
 * Create a WPForms Smart Tag to pull User role from their profile.
 *
 * @link   https://wpforms.com/developers/how-to-create-more-user-smart-tags/
 */

function wpf_user_role_smart_tag( $tags ) {
	
	$tags[ 'wp_user_role' ] = 'WP Profile User Role'; //used all over
	
	return $tags;
}

add_filter( 'wpforms_smart_tags', 'wpf_user_role_smart_tag' );

function wpf_user_role_process_smart_tag($content, $tag ) {
	
	switch ($tag) {
			
		case "wp_user_role":
			
		$user = wp_get_current_user();
			
		$roles = implode(', ', (array) $user->roles);
			
		$content = str_replace( '{' . $tag . '}', $roles, $content );
			
		break;
	}
	
	return $content;
}

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