Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Danny van Kooten

    (@dvankooten)

    Hi Loadzz,

    The process for this is actually quite similar to the one described here: How to force Buddypress Registration Tick Box Ticked?

    Here’s a code snippet that applies to ALL integrations.

    add_filter( 'mc4wp_integration_checkbox_attributes', function( array $attributes, $integration ) {
    	$attributes['required'] = 'required';
    	return $attributes;
    }, 10, 2);

    Source: https://github.com/ibericode/mc4wp-snippets/blob/master/integrations/force-checkbox-to-be-checked.php

    If you want to target just Contact Form 7, please use the following.

    add_filter( 'mc4wp_integration_contact-form-7_checkbox_attributes', function( array $attributes, $integration ) {
    	$attributes['required'] = 'required';
    	return $attributes;
    }, 10, 2);

    This code can be added to your theme its functions.php file or dropped into a plugin of its own.

    Hope that helps. If not, let me know!

    Thread Starter Loadzz

    (@loadzz)

    Hello Danny,

    Thanks for your help – I’ve added the Contact Form 7 code you provided to my theme functions, but it still lets me send the form without the mailchimp box checked.

    Any other solutions?

    Thanks,

    Thread Starter Loadzz

    (@loadzz)

    {bump}

    Thanks,

    Plugin Contributor Harish Chouhan

    (@hchouhan)

    Hey Loadzz,

    Sorry for the late reply. Can you please share a link to your website so we could check?

    Thread Starter Loadzz

    (@loadzz)

    Is there an email address i can send you my website link to?

    Thanks,

    Plugin Contributor Harish Chouhan

    (@hchouhan)

    JHey Loadzz,

    Sure. you can email us at “support @ mc4wp.com”. Please make sure to include all the details and a link to this post.

    I’ll mark this post as resolved so we can discuss directly over email.

    Thread Starter Loadzz

    (@loadzz)

    Sent the email – Thanks!

    Hello,

    I had the same experience. The code above did not work. Is there a solution for this problem.

    Plugin Author Danny van Kooten

    (@dvankooten)

    Hi everyone,

    Sorry for the late reply, it turns out that Contact Form 7 disables HTML5 validation by default so we need to do a little more to get this to work.

    Here’s the code that should do the trick!

    /**
     * By default, Contact Form 7 disables HTML5 validation so we can not use the default <code>required</code> attribute.
     *
     * This code uses CF7 logic to ensure that the subscribe checkbox is checked.
     */
    add_filter( 'wpcf7_acceptance', function( $yes ) {
    	if( ! $yes ) { return false; }
    	return ! empty( $_POST['_mc4wp_subscribe_contact-form-7'] );
    });

    Github: https://github.com/ibericode/mc4wp-snippets/blob/master/integrations/contact-form-7/require-checkbox.php

    To make this a little more user friendly you can also use the snippet I shared earlier but with a minor modification. This enables client-side validation, so the form doesn’t have to be submitted before seeing the error.

    <?php
    
    /**
     * Adds "required" attribute to the HTML for the sign-up checkbox.
     *
     * This forces the checkbox to be checked.
     *
     * @param array $attributes
     * @param MC4WP_Integration $integration
     * @return array
     */
    add_filter( 'mc4wp_integration_checkbox_attributes', function( array $attributes, $integration ) {
    	$attributes['required'] = 'required';
    	return $attributes;
    }, 10, 2);
    
    /**
     * If you're running Contact Form 7, please also include the following line to enable HTML5 validation.
     */
    add_filter( 'wpcf7_form_novalidate', '__return_false' );

    Github: https://github.com/ibericode/mc4wp-snippets/blob/master/integrations/require-checkbox-to-be-checked.php

    Hope that helps. If not, let me know!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Required field in Contact Form 7’ is closed to new replies.