您可以使用提供的过滤器来执行所有操作:
//add your checkbox after the comment field
add_filter( \'comment_form_field_comment\', \'my_comment_form_field_comment\' );
function my_comment_form_field_comment( $comment_field ) {
return $comment_field.\'<input type="checkbox" name="privacy" value="privacy-key" class="privacyBox" aria-req="true"><p class="pprivacy">Hiermit akzeptiere ich die <a target="blank" href="http://wp.cloudstarter.de/?page_id=156">Datenschutzbedingungen</a><p>\';
}
//javascript validation
add_action(\'wp_footer\',\'valdate_privacy_comment_javascript\');
function valdate_privacy_comment_javascript(){
if (is_single() && comments_open()){
wp_enqueue_script(\'jquery\');
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$("#submit").click(function(e){
if (!$(\'.privacyBox\').prop(\'checked\')){
e.preventDefault();
alert(\'You must agree to our privacy term by checking the box ....\');
return false;
}
})
});
</script>
<?php
}
}
//no js fallback validation
add_filter( \'preprocess_comment\', \'verify_comment_privacy\' );
function verify_comment_privacy( $commentdata ) {
if ( ! isset( $_POST[\'privacy\'] ) )
wp_die( __( \'Error: You must agree to our privacy term by checking the box ....\' ) );
return $commentdata;
}
//save field as comment meta
add_action( \'comment_post\', \'save_comment_privacy\' );
function save_comment_privacy( $comment_id ) {
add_comment_meta( $comment_id, \'privacy\', $_POST[ \'privacy\' ] );
}