Created
February 19, 2018 11:54
-
-
Save perezdans/a5b404fc9d09497ddfa760011b7e151d to your computer and use it in GitHub Desktop.
Añadir casilla de aceptación de la política de privacidad en los comentarios de WordPress con un código. Necesario para cumplir la RGPD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Casilla de verificación de privacidad después del formulario de comentarios */ | |
add_filter( 'comment_form_field_comment', 'mi_campo_de_privacidad_en_comentarios' ); | |
function mi_campo_de_privacidad_en_comentarios( $comment_field ) { | |
return $comment_field.'<label><input type="checkbox" name="privacy" value="privacy-key" class="privacyBox" aria-req="true"> Acepto la <a target="blank" href="https://ayudawp.com/legal/">política de privacidad</a></label>'; | |
} | |
//validación por javascript | |
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('Debes aceptar nuestra política de privacidad marcando la casilla ....'); | |
return false; | |
} | |
} | |
}); | |
</script> | |
<?php | |
} | |
} | |
//sin validación js | |
add_filter( 'preprocess_comment', 'verify_comment_privacy' ); | |
function verify_comment_meta_data( $commentdata ) { | |
if ( ! isset( $_POST['privacy'] ) ) | |
wp_die( __( 'Error: Debes aceptar nuestra política de privacidad marcando la casilla ....' ) ); | |
return $commentdata; | |
} | |
//guarda el campo como comment meta | |
add_action( 'comment_post', 'save_comment_privacy' ); | |
function save_comment_meta_data( $comment_id ) { | |
add_comment_meta( $comment_id, 'privacy', $_POST[ 'privacy' ] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment