John Davidson

php - Get username that submitted a form in wp contact form 7?

0 comments
Message:


how can I get the username that submitted a form created with contact form 7?
I know there I can set it as default and hide it, but then users can change it and submit it of course (I need it to be uneditable).


So is there any way I can get in Flamingo plugin the username of the person who submitted the form? Or any suggestion?


I created the form with the plugin editor, so I create the fields like this:


<p> Username
[text* your-name default:user_login] </p>

<label> Email
[email* your-email] </label>

<label> Passaport
[text* your-subject] </label>

And then I use the shortcode to insert the form:


[contact-form-7 id="127" title="Formulario de contacto 1"]

But by getting the username by this mean, it can be of course edited by the user.



My Answer:
Unfortunately, Contact Form 7 does not have a built-in feature to capture the username of the person submitting the form. However, you can achieve this by using a combination of Contact Form 7 and Flamingo plugin.

One way to capture the username is to add a hidden field in your Contact Form 7 form that will automatically populate with the username of the logged-in user. You can use the following code snippet to achieve this:

1. Add the following code to your theme's functions.php file:

php
add_action('wpcf7_init', 'add_username_field');

function add_username_field() {
wpcf7_add_form_tag('username', 'username_form_tag_handler');
}

function username_form_tag_handler($tag) {
if (!is_user_logged_in()) {
return '';
}

$current_user = wp_get_current_user();
$username = $current_user->user_login;

$atts = array();
$atts['type'] = 'hidden';
$atts['name'] = 'username';
$atts['value'] = $username;

$html = sprintf('', wpcf7_format_atts($atts));

return $html;
}


2. Add the following shortcode to your Contact Form 7 form where you want the hidden username field to appear:


[username]


This code will automatically populate the hidden field with the username of the logged-in user. Users will not be able to edit this field before submitting the form.

After the form is submitted, you can view the submitted data in the Flamingo plugin, where you will be able to see the username of the person who submitted the form.

Please note that this solution assumes that users are logged in when submitting the form. If users are not required to be logged in, you may need to implement a different solution to capture the username.

Rate this post

5 of 5 based on 7044 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us