samedi 4 mai 2019

How can I make the Wordpress comment checkbox mandatory/required before posting a comment?

I disabled the Wordpress function 'Show comments cookies opt-in checkbox, allowing comment author cookies to be set.' but I added a checkbox in the comment form manually because I wanted to change the label of the checkbox.

I did this by adding the following code to the functions.php of my child theme:

add_filter( 'comment_form_default_fields', 'tu_comment_form_change_cookies_consent' );
function tu_comment_form_change_cookies_consent( $fields ) {
    $commenter = wp_get_current_commenter();

    $consent   = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';

    $fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' />' .
                     '<label for="wp-comment-cookies-consent">By using this comment form you agree with our Privacy Policy</label></p>';
    return $fields;

}

This is working fine but now I wanna have this checkbox mandatory so that the user has to check it before pressing the 'Post comment' button.

So if the checkbox is unchecked, the user should see a error message when clicking on the 'Post comment' button.

How can I do that? All the suggestions I found so far are not working, like for example adding 'required' behind the input id or name.

Thanks for your help!




Aucun commentaire:

Enregistrer un commentaire