Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?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