Carry over ACF field when Contact Form 7 is submitted

85 Views Asked by At

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

1

There are 1 best solutions below

0
On

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);
}