Created
February 22, 2018 09:52
-
-
Save fbmoises/79bdc2cb05a9e3ee66cb288fb96e8fd2 to your computer and use it in GitHub Desktop.
Añadir casilla de aceptación de la política de privacidad en los comentarios con un código
This file contains 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
/* 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.'<input type="checkbox" name="privacy" value="privacy-key" class="privacyBox" aria-req="true"><p class="pprivacy">Acepto la <a target="blank" href="https://ayudawp.com/legal/">política de privacidad</a><p>'; | |
} | |
//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