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!