How to Create More User Smart Tags

Overview

Would you like to create more WordPress user Smart Tags to use in your WPForms? 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. This tutorial will show you how to create two new Smart Tags to be used by pulling information from the WordPress user profile.

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

This tutorial will show you how to create two new Smart Tags for the WordPress User Nickname and the WordPress Username.

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.

Setup

In the code snippet below, you’ll see there are two functions. One will create the Smart Tag(s) and the other will provide the ability to add the new Smart Tag(s) to the list in the form builder.

Simply add this code to your site.

/**
 * Create a WPForms Smart Tag to pull Nickname and Username fields from WordPress profile.
 * Register the Smart Tags so it will be available to select in the form builder.
 *
 * @link   https://wpforms.com/developers/how-to-create-more-user-smart-tags/
 * 
 * @param  array $tags
 * @return array
 */
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/
 *
 * @param  string $content
 * @param  string $tag
 * @return string
 */
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 );

And after saving the code, you’ll now see the two new Smart Tags in your form builder.

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.

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.

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