0

I need to generate CF7 fields from php to make the form dynamic.

So instead of writing <div>[text* your-name]</div> and so on directly in wordpress, I have a template file that does that instead. The problem is that when doing so, the validation is not working. The fields are displayed correctly, but no fields are checked when submitting. If submitting, the data is however saved correctly.

WP ADMIN form tab:

[my_cf_template]

template.php

<?php echo do_shortcode(apply_filters("the_content", '[contact-form-7 id="115" title="Ruumide rent - booking"]')); ?>

functions.php

function cf_template_func(){

    $email = wpcf7_do_shortcode('[email* your-email]');
    $submit = wpcf7_do_shortcode( '[submit "Send"]' );

    $str = <<<HTML


    <div class="detailed-info">

        <label> Your E-mail*
        $email </label>

    </div>  
    $submit     
    HTML;
    return $str;
}

add_action( 'wpcf7_init', 'custom_add_shortcode');

function custom_add_shortcode() {
    wpcf7_add_shortcode( 'my_cf_template', 'cf_template_func');
}
Mes
  • 34
  • 4
  • do you get any erro?? – Vasim Shaikh Dec 01 '16 at 11:49
  • No errors/ When submitting, its submits the form correctly and pops up success message. So functionality is working but fields are not being validated. So theoretically if you don't fill any fields you can still submit and get success. – Mes Dec 01 '16 at 11:52
  • okay can you provide url here?? – Vasim Shaikh Dec 01 '16 at 11:53
  • Fortunately I cant, since it's only on my localhost. But what were you going to test? I could try myself – Mes Dec 01 '16 at 12:02
  • do you have Validation Filter?? – Vasim Shaikh Dec 01 '16 at 12:03
  • I think you have to check this,http://contactform7.com/2015/03/28/custom-validation/ – Vasim Shaikh Dec 01 '16 at 12:03
  • no manually i havn't set any filters in regards of validation, all I have, is written above – Mes Dec 01 '16 at 12:04
  • I'll check it out, thanks – Mes Dec 01 '16 at 12:05
  • okay,check and implement there is a method call `invalidate()` you have to focus on that – Vasim Shaikh Dec 01 '16 at 12:06
  • Okay so i tried this example on the link you shared. It still didnt do anything. It seems like submitting form doesnt even run any kind of validation functkion. (when i tested the example while putting shortcodes in admin, it worked so I know I didnt make mistakes, but mistake is still somewhere else). So looks like wpcf7_validate_email* didnt even run – Mes Dec 01 '16 at 12:18
  • check console do you have any error in console.log ? – Vasim Shaikh Dec 01 '16 at 12:22
  • No errors, and network logs shows "{"mailSent":true,"into":"#wpcf7-f115-o1","captcha":null,"message":"Thank you for your message. It has been sent."}" – Mes Dec 01 '16 at 12:26
  • Maybe im initializing something too early or too late in my php files? I tried to use different hooks but no positive results. – Mes Dec 01 '16 at 12:27
  • Is this your custom template for CF7?? – Vasim Shaikh Dec 01 '16 at 12:46
  • Yes. Custom template where im basically defining all the layout of the form. Then using that [my_cf_template] shortcode to "require" all the layout from php to cf7 admin. It looks to me like it doesnt validate the fields because they dont directly exist in the wp-admin, So if i could maybe find a way to manually in php set all the fields that need to be validated i think it should work. I just dont know how to – Mes Dec 01 '16 at 12:56

0 Answers0