Plugin Planet

Premium WordPress Plugins

This tutorial explains how to customize the name of the post author when submitting posts via the front-end with USP Pro. The first technique shows how to use the Display Name instead of the default Login Name when using the default Name field. And the second technique shows how to get the user’s name via a custom field.

Replace Login Name with Display Name

By default, USP Pro auto-fills the Name field ([usp_name]) with the user’s Login Name (if they are logged in). To change this to the user’s Display Name instead, add the following code to your theme‚Äôs functions.php:

// usp pro replace login name with display name
function usp_pro_modify_name($name) {
	
	if (!($current_user instanceof WP_User)) return $name;
	
	$user = wp_get_current_user();
	$display_name = $user->display_name;
	
	return $display_name;
	
}
add_filter('usp_input_name_value', 'usp_pro_modify_name');

This code simply grabs the current user object and returns the display_name, which then will be used as the auto-filled value of the Name field. If you prefer, you can change the display_name with one of the other name formats, like user_firstname or user_lastname. Check out the WP Codex for more infos.

Get the user’s name via custom field

The default Name field is great because it stores the user’s name as the Post Author, which most themes display along with each post. So using the default Name field in your forms is the recommended way of collecting the user’s name, but it’s also possible to collect the user’s name using a custom field. Using a custom field enables you to implement custom functionality according to your needs.

To use a custom field to collect the (logged-in) user’s name, follow these steps:

Step 1

On your form, define a custom field like this:

name#user-name|for#user-name|data-required#true|label#Name|placeholder#Name

You can customize the custom field attributes however you wish; the important thing is to use a specific value for the name attribute. In this example, we are using user-name, which will be used in the final step of this tutorial.

Step 2

After defining and saving your custom field, add it to your form via its corresponding shortcode, which will look something like this:

[usp_custom_field form="123" id="1"]

Note that this is just an example, your custom-field shortcode will probably have different form and id values.

Step 3

After the custom field is added to your form, add the following code to your theme’s functions.php:

// usp pro get name via custom field
function usp_pro_use_display_name($atts) {
	
	$user = wp_get_current_user();
	$display_name = $user->display_name;
	
	if (isset($atts['value'])) {
		if ($atts['name'] === 'user-name') {
			$atts['value'] = $display_name;
		}
	}
	
	return $atts;
	
}
add_filter('usp_custom_field_atts', 'usp_pro_use_display_name');

This code grabs the current logged-in user’s display_name, and then uses it to replace the default value of the custom field. So when the user is logged in and visits the form, the custom field will be auto-filled with the user’s Display Name.

Notice in the above code that we are checking for a value of user-name for the name field. This is the same name value that we defined for the custom field in Step 1. So make sure that they match up in order for this technique to work.

Also, as with the previous technique, you can change the display_name with one of the other name formats, like user_firstname or user_lastname. Check out the WP Codex for more infos.

Make it a hidden field

One of the downsides of auto-filling the Name field is that it’s possible for the user to change it before submitting the form. To prevent this, and to always make sure that the user’s registered information is used to get their name, you can make the Name field hidden. This means that auto-fill value will be added to the form behind the scenes, so that the correct user name always will be submitted along with each post.

Hide the default Name field

To hide the Name field when using the [usp_name] shortcode, add the custom attribute to the shortcode, like so:

[usp_name label="null" custom="style='display:none;'"]

You can verify the inclusion of this field by submitting a test post and checking the Post Author’s name, which should be correct according to the user’s registered information.

Hide the custom Name field

To hide the Name field when using a custom field, add the field and type attributes to the custom-field definition, like so:

field#input|type#hidden|name#user-name|for#user-name|data-required#true|label#null|placeholder#null|fieldset#null

Notice also that we disable the field label, placeholder, and fieldset by using null as their respective attribute values. We do this because these attributes/tags are not required for hidden inputs. Examine the source code of your form’s web page for more information.

As before, you can verify the inclusion of this field by submitting a test post and checking the Post Author’s name, which should be correct according to the user’s registered information.

Learn more about USP Pro hidden fields.