How To Get Password Fields In Contact Form 7

Every time when I develop a WordPress site, I am yet again left searching for the best WordPress contact form plugin. While most of the WordPress themes have built-in contact forms. Sometimes it’s often better to create your own contact form with a WordPress plugin because a good contact form gives your website a professional appearance.
It helps you manage your online communication with your clients or visitors.

Get Password fields in contact form 7

Contact form 7 is one of the best and most popular plugin.

Contact Form 7 can manage multiple contact forms, plus you can customize the form and the mail contents flexibly with simple markup.

I used this plugin for one of my client’s project. Added some contact forms and found that the password field is missing. Then I searched the google and found some codes to add a password field in contact form 7.

In this tutorial, I am going to share how I managed to get a password field in contact form 7.

I found a function by which we we can get the password fields in Contact Form 7.

Get Password Fields In Contact Form 7

First of all add this below code to your theme’s functions.php file.

function cfp($atts, $content = null) {
    extract(shortcode_atts(array( "id" => "", "title" => "", "pwd" => "" ), $atts));

    if(empty($id) || empty($title)) return "";

    $cf7 = do_shortcode('

Error: Contact form not found.

'); $pwd = explode(',', $pwd); foreach($pwd as $p) { $p = trim($p); $cf7 = preg_replace('/<input type="text" name="' . $p . '"/usi', '<input type="password" name="' . $p . '"', $cf7); } return $cf7; } add_shortcode('cfp', 'cfp');

Now we create a form with contact form 7 plugin with the following three fields:
username
email and
password

<p>Your Username (required)<br />
   [text* username] </p>

<p>Your Email (required)<br />
  [email* email] </p>

<p>Your Password<br />
  [text* password] </p>

<p>[submit "Register"]</p>

Now if we hide password field of the contact form then write following short code into your post:

[cfp id="7" title="Test" pwd="password"]

Remember to change the ‘id’ and ‘title’ value according to your form.

register-smallenvelop

That’s it, we have created a password field in contact form 7 plugin. Please comment if you have any queries.

20 thoughts on “How To Get Password Fields In Contact Form 7”

  1. Thanks for the tip on this. I’ve looked at several other solutions and this was the simplest to implement. I’ve got it working, but my question is,what if I wanted two different password fields in the same form?

    For example, I have a form that asks for FTP login credentials and then also WordPress Admin login credentials. The input for each password field would be different, but I would like to mask them both in the same form. Any ideas how I could go about that? Thanks in advance!

    Reply
  2. hello,
    I need help in adding password field
    i added the code in themes functions.php but password file showing text type and unable to generate the password field

    Reply
  3. Hello, can i use this trick with password an confirm_password ?? i want to pass two password fields to this function, how can i do please ??

    Reply
    • Thanks Poonam for the post! Unfortunately I didn’t get it to work with the given shortcode.
      Question to @GMK Hussain: I’m trying to implement the password field with the shortcodes you provided. In the github page it says the following:
      //Add this jQuery code in footer file. * Tested is working.
      jQuery(“[name*=’password’]”).attr(“type”, “password”);

      Where do I have to insert that code?!
      Thanks!

      Reply
  4. Hi i have added code in unctions created new form but i keep get [contact-form-7 404 "Not Found"].

    What do i need to do. Thanks

    Reply
    • I’m not sure if you have figured this out already.

      Try changing the function in your theme’s functions.php file.

      /* Add field type password in contact form 7 */
      function cfp($atts, $content = null) {
      extract(shortcode_atts(array( “id” => “”, “title” => “”, “pwd” => “” ), $atts));

      if(empty($id) || empty($title)) return “”;

      $cf7 = do_shortcode(‘[contact-form-7 id="' . $id . '" title="' . $title . '"]‘);

      $pwd = explode(‘,’, $pwd);

      foreach($pwd as $p) {
      $p = trim($p);

      $cf7 = preg_replace(‘/<input type="text" name="' . $p . '"/usi', '<input type="password" name="' . $p . '"', $cf7);
      }

      return $cf7;
      }
      add_shortcode('cfp', 'cfp');

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.