Last active
February 9, 2017 19:28
-
-
Save jeffward3283/50b9a0b75e31de51661b to your computer and use it in GitHub Desktop.
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
<?php | |
/////////////////////////////////// | |
# Enqueue Human Comment Script | |
/////////////////////////////////// | |
add_action('wp_enqueue_scripts', 'add_human_comment_script'); // Add Theme Stylesheet | |
function add_human_comment_script(){ | |
wp_register_script('human_comments', admin_url() . 'admin-ajax.php?action=human_comments_script', array('jquery'), '1.0', true); | |
wp_enqueue_script('human_comments'); | |
} | |
/////////////////////////////// | |
# Human Comment Script | |
/////////////////////////////// | |
add_action('wp_ajax_nopriv_'.'human_comments_script', 'human_comments_script'); | |
add_action('wp_ajax_'.'human_comments_script', 'human_comments_script'); | |
function human_comments_script(){ | |
# Begin Output button | |
ob_start(); | |
# Display the output | |
?> | |
<script> | |
jQuery(document).ready(function($){ | |
$(window).on('load reloaded', function(){ | |
$('#respond form').not('.human-binded').each(function(){ | |
var $form = $(this).addClass('human-binded'); | |
$form.append('<input type="hidden" name="my_secret" value="1" />'); | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
# Capture the output | |
$output = ob_get_contents(); | |
ob_end_clean(); | |
# Remove script tags | |
$output = str_replace('<script>', '', $output); | |
$output = str_replace('</script>', '', $output); | |
# Output the results | |
header('Content-Type: text/javascript'); | |
echo $output; | |
exit(); | |
} | |
/////////////////////////////// | |
# Filter Comment Submit | |
/////////////////////////////// | |
add_action('wp_insert_comment', 'remove_non_human_comments', 10, 2); | |
function remove_non_human_comments( $id, $comment){ | |
if(empty($_POST['my_secret'])){ | |
wp_delete_comment($id, $force_delete = true); // Delete the inserted comment | |
$error = __('<strong>Something wrong happened to your comment submission!</strong> <br />Please make sure you have JavaScript enabled and try again.', TEXTDOMAIN); | |
wp_die( $error ); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment