Skip to content

Instantly share code, notes, and snippets.

@elhardoum
Last active July 2, 2016 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elhardoum/cf809c790a619c03843de3d239cdce43 to your computer and use it in GitHub Desktop.
Save elhardoum/cf809c790a619c03843de3d239cdce43 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: No Spam Registration with JavaScript
* Plugin URI: http://blog.samelh.com
* Description: Prevents spam registration on your WordPress blog/website by adding a necessary form field with JavaScript on document load
* Author: Samuel Elh
* Author URI: http://samelh.com
* Version: 0.1
*/
add_action('register_form', 'se_nospam_register_append_input');
add_action('register_post', 'se_nospam_register_validate', 10, 3);
if ( !function_exists('se_nospam_register_append_input') ) :;
function se_nospam_register_append_input()
{
?>
<script type="text/javascript" id="se_nospam_inline_js">
window.onload = function() { // it's all about this JS, once JS is loaded, the spamcheck field will be available..
var e = document.getElementById('se_nospam_inline_js');
if ( null !== e ) {
e.outerHTML = '<input id="process-register" type="hidden" name="process-register" value="<?php echo wp_create_nonce( 'se-nospam-register' ); ?>" />';
} return;
}
</script>
<?php
}
endif;
if ( !function_exists('se_nospam_register_validate') ) :;
function se_nospam_register_validate( $login, $email, $errors )
{
$die_message = apply_filters( "se_nospam_register_error", "Are you spamming?<br/><br/> <a href=\"javascript: window.history.go(-1);\">&laquo; Go back</a>" );
if( !isset($_POST['process-register']) ) {
wp_die( $die_message );
// or just: $errors->add( 'empty_realname', "<strong>ERROR</strong>: Are you spamming?" );
}
else if( empty($_POST['process-register']) )
{
wp_die( $die_message );
// or just: $errors->add( 'empty_realname', "<strong>ERROR</strong>: Are you spamming?" );
}
else if(!wp_verify_nonce($_POST['process-register'], 'se-nospam-register'))
{
wp_die( $die_message );
// or just: $errors->add( 'empty_realname', "<strong>ERROR</strong>: Are you spamming?" );
}
return $errors;
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment