skip to Main Content

Just a brief explanation; Currently I have a site where employees login and are able to submit forms to request services. Each employee has a unique ID field that was created using ACF fields. What I would like to happen is that when they submit a form requesting services that this unique ID passes through as a hidden field and is shown on the submission result in Contact Form 7. This would drastically help our company be more productive and reduce time wasted.

Currently I’ve tried a few different options and nothing has worked, curious if anyone has dealt with a similar issue.

Thank you,
Dilion Smith

2

Answers


  1. Someone else might come up with a more elegant or better solution but one possible way would be to echo the current employee id onto the page in js and then replace the cf7 hidden form value with it.

    Assuming your hidden field is like so.

    [hidden employee_id default:"0"]
    

    In your functions.php file you can create the variable that actually stores the employee id and put it on the page.

    function echo_cf7_employee_id() {
        $post = get_post();
        if (has_shortcode( $post->post_content, 'contact-form-7')) {
            echo '<script type="text/javascript">let cf7_employee_id = '.get_field('employee_id', 'user_'. get_current_user_id()).';</script>';
        }
    }
    add_action('wp_footer', 'echo_cf7_employee_id');
    

    In one of your js files you can then add the following to check if that field exists on the current page and then update its value with your employee id.

    if($('input[name="employee_id"]').length) {
        $('input[name="employee_id"]').val(cf7_employee_id);
    }
    
    Login or Signup to reply.
  2. To carry over a user’s Advanced Custom Fields (ACF) value into a Contact Form 7 (CF7) submission, you can use a hidden field within your CF7 form that is populated dynamically using a shortcode that retrieves the current user’s ACF field value. Follow these steps to set it up:

    1. Create a Shortcode to Get the User’s ACF Field Value:

      Add the following code to your theme’s functions.php file or a custom functionality plugin:

      add_shortcode('acf_user_id', 'get_acf_user_id');
      function get_acf_user_id() {
          $user_id = get_current_user_id();
          if (empty($user_id)) {
              return ''; // Return empty if user is not logged in.
          }
          return get_field('unique_id', 'user_' . $user_id); // Replace 'unique_id' with your ACF field name.
      }
      

      This shortcode ([acf_user_id]) will return the value of the ACF field for the currently logged-in user.

    2. Add a Hidden Field to Your CF7 Form:

      Edit your CF7 form and add a hidden field that uses this shortcode to populate its value. You can do this by adding the following tag to the form:

      [hidden user-unique-id "[acf_user_id]"]
      

      Make sure to replace user-unique-id with a name that will help you identify this field in the email that CF7 sends after form submission.

    3. Configure Your CF7 Mail:

      When setting up the mail template in CF7, include the hidden field to carry the value through to the email. In the "Message body" section of the mail tab, include the hidden field like so:

      Employee ID: [user-unique-id]
      

      This will add the unique ID to the email that is sent upon form submission.

    4. Test the Form:

      Make sure you test the form with a logged-in user who has a unique ID set in their user profile in ACF. When they submit the form, the hidden field should carry over their unique ID to the email you receive.

    Remember to replace 'unique_id' with the actual field name you used in ACF for the unique ID. If the unique ID is mandatory for all users, you might want to add a check to ensure the user has it set before they can submit the form.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search