Dynamicly populate Contact form 7 input fields with current user info when logged in in Wordpress website

后端 未结 4 1350
暖寄归人
暖寄归人 2021-01-21 18:12

I\'ve been trying to figure out how to populate input fields in contact forms on my Wordpress website. I\'ve tried using a plugin called Contact Form 7 dynamic text extension, a

相关标签:
4条回答
  • 2021-01-21 18:43

    You can install this plugin

    Contact Form 7 Dynamic Text Extension

    Dynamic text and dynamic hidden fields are available using it. And you can use the following code to grab the current user info into your field.

    [dynamictext dynamicname "CF7_get_current_user"]
    

    Or as mentioned in the answer above:

    [dynamictext your-email "CF7_get_current_user key='user_email' "]
    

    Note also that you can use dynamichidden for hidden fields like the following

    [dynamichidden your-email "CF7_get_current_user key='user_email' "]
    

    PS: I faced an issue with required hidden fields when I tried to use this dynamichidden*, the shortcode appears on my website as a plain text.

    0 讨论(0)
  • 2021-01-21 18:48

    Turns out, i wasn't using the shortcode correctly. These are the shortcodes i've used in with Contact Form 7 Dynamic text extension:

        Naam* <br />
         [dynamictext* your-name 'CF7_get_current_user']
        Email*
         [dynamictext* your-email "CF7_get_current_user key='user_email' "]
    
    0 讨论(0)
  • 2021-01-21 18:50

    Let's say you have an PHP array $gigs, and you want to display it as a drop down list in your form like this:

    <label>
      <strong>Choose your gig</strong>
      [select upcoming-gigs data:gigs]
    </label>
    [submit]
    

    Simply add this to your functions.php file

    add_filter('wpcf7_form_tag_data_option', function($n, $options, $args) {
      if (in_array('gigs', $options)){
        $gigs = array(
          "MAY 07 - NEW ORLEANS, LA",
          "MAY 09 - AUSTIN, TX",
          "MAY 12 - HOUSTON, TX"
        );
        return $gigs;
      }
      return $n;
    }, 10, 3);
    

    Now your drop down field should look like this:

    Source: https://bdwm.be/dynamically-populate-a-contact-form-7-dropdown-list-or-any-other-input-field/

    0 讨论(0)
  • 2021-01-21 19:03

    I have done this before. Instead of using the PHP code as a placeholder. Use jQuery, for example:

    $firstname = $current_user->user_firstname;
    
    <script>
    
    $("input").val("<?php echo $firstname; ?>")
    
    </script>
    
    0 讨论(0)
提交回复
热议问题