This shortcode returns the username of the currently logged-in user. If the user is not logged in, it will be blank.
[CF7_get_current_user]
This shortcode includes an optional key attribute where you can specify which variable of the currently logged-in user you want to output.
[CF7_get_current_user key="<insert user field key>"]
Example
If I wanted to get the email of the current user, the value of my dynamic field would use this:
CF7_get_current_user key='user_email'
And then the dynamic text form tag would look like this:
[dynamictext inputname "CF7_get_post_var key='post_title'"]
Possible Options
Any property that is available from the object returned from wp_get_current_user() (type: WP_User) and returns a string or numeric value can be used such as:
- User ID:
CF7_get_current_user key='ID' - Email address:
CF7_get_current_user key='user_email' - First name:
CF7_get_current_user key='user_firstname' - Last name:
CF7_get_current_user key='user_lastname' - Display name:
CF7_get_current_user key='display_name'
It is also possible to use this shortcode to access user meta data because this uses the get() function from the WP_User class.
Contact Form 7 Native Functionality
In a later version of Contact Form 7, the default:{source} option was introduced for form tags. This native feature in Contact Form 7 may make it seem like our DTX shortcode is redundant, but not quite. In order for their method to work, the name of your form tag needs to match that of the key from your source. Using the same example from above, the form tag would look like this:
[text inputname default:user_email]
While we’re glad to see this functionality make it into the core of Contact Form 7, DTX offers the flexibility for pulling user meta data, mapping keys to form fields with different names, adding security through obfuscating the value, and compatibility with caching plugins.