'; } function registration_css() { global $recaptcha_opt; if ($recaptcha_opt['re_registration']) { $width = 0; if ($recaptcha_opt['re_theme_reg'] == 'red' || $recaptcha_opt['re_theme_reg'] == 'white' || $recaptcha_opt['re_theme_reg'] == 'blackglass') $width = 358; else if ($recaptcha_opt['re_theme_reg'] == 'clean') $width = 485; echo << #login { width: {$width}px !important; } #login a { text-align: center; } #nav { text-align: center; } REGISTRATION; } } add_action('wp_head', 're_css'); // include the stylesheet in typical pages to style hidden emails add_action('admin_head', 're_css'); // include stylesheet to style options page add_action('login_head', 're_css'); // include stylesheet to style reCAPTCHA on registration page add_action('login_head', 'registration_css'); // include the login div styling, embedded /* ============================================================================= End CSS ============================================================================= */ // If the plugin is deactivated, delete the preferences function delete_preferences() { delete_option('recaptcha'); } register_deactivation_hook(__FILE__, 'delete_preferences'); /* ============================================================================= reCAPTCHA on Registration Form - Thanks to Ben C.'s recapture plugin ============================================================================= */ // Display the reCAPTCHA on the registration form function display_recaptcha($errors) { global $recaptcha_opt; if ($recaptcha_opt['re_registration']) { $format = << var RecaptchaOptions = { theme : '{$recaptcha_opt['re_theme_reg']}', lang : '{$recaptcha_opt['re_lang']}' , tabindex : 30 }; END; $comment_string = << document.getElementById('recaptcha_table').style.direction = 'ltr'; COMMENT_FORM; if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") $use_ssl = true; else $use_ssl = false; if ($wpmu == 1) { $error = $errors->get_error_message('captcha'); ?> > is_valid) if ($response->error == 'incorrect-captcha-sol') $errors['captcha_wrong'] = 'That reCAPTCHA was incorrect.'; } } // Check the captcha function check_recaptcha_new($errors) { global $recaptcha_opt; if (empty($_POST['recaptcha_response_field']) || $_POST['recaptcha_response_field'] == '') { $errors->add('blank_captcha', 'Please complete the reCAPTCHA.'); return $errors; } $response = recaptcha_check_answer($recaptcha_opt['privkey'], $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field'] ); if (!$response->is_valid) if ($response->error == 'incorrect-captcha-sol') $errors->add('captcha_wrong', 'That reCAPTCHA was incorrect.'); return $errors; } // Check the recaptcha on WordPress MU function check_recaptcha_wpmu($result) { global $_POST, $recaptcha_opt; if (empty($_POST['recaptcha_response_field']) || $_POST['recaptcha_response_field'] == '') { $result['errors']->add('blank_captcha', 'Please complete the reCAPTCHA.'); return $result; } $response = recaptcha_check_answer($recaptcha_opt['privkey'], $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field'] ); if (!$response->is_valid) if ($response->error == 'incorrect-captcha-sol') { $result['errors']->add('captcha_wrong', 'That reCAPTCHA was incorrect.'); echo "
Incorrect CAPTCHA
"; } return $result; } if ($recaptcha_opt['re_registration']) { if ($wpmu == 1) add_filter('wpmu_validate_user_signup', 'check_recaptcha_wpmu'); else { // Hook the check_recaptcha function into WordPress if (version_compare(get_bloginfo('version'), '2.5' ) >= 0) add_filter('registration_errors', 'check_recaptcha_new'); else add_filter('registration_errors', 'check_recaptcha'); } } /* ============================================================================= End reCAPTCHA on Registration Form ============================================================================= */ /* ============================================================================= reCAPTCHA Plugin Default Options ============================================================================= */ $option_defaults = array ( 'pubkey' => '', // the public key for reCAPTCHA 'privkey' => '', // the private key for reCAPTCHA 'use_mailhide' => '0', // whether or not to use mailhide 're_noadmins' => '0', // display reCAPTCHA for admins? 'mh_noadmins' => '0', // hide emails from admins? 'mailhide_pub' => '', // mailhide public key 'mailhide_priv' => '', // mailhide private key 're_theme' => 'red', // the default theme for reCAPTCHA on the comment post 're_theme_reg' => 'red', // the default theme for reCAPTCHA on the registration form 're_lang' => 'en', // the default language for reCAPTCHA 're_tabindex' => '5', // the default tabindex for reCAPTCHA 're_comments' => '1', // whether or not to show reCAPTCHA on the comment post 're_registration' => '1', // whether or not to show reCAPTCHA on the registratoin page 're_xhtml' => '0', // whether or not to be XHTML 1.0 Strict compliant ); // install the defaults add_option('recaptcha', $option_defaults, 'reCAPTCHA Default Options', 'yes'); /* ============================================================================= End reCAPTCHA Plugin Default Options ============================================================================= */ /* ============================================================================= MailHide - This scans for addresses and hides them using the MailHide API ============================================================================= */ // The main mailhide filter function mh_insert_email($content = '') { global $recaptcha_opt; // we're not hiding emails from admins if ($recaptcha_opt['mh_noadmins'] && current_user_can('level_10')) return $content; // Regular Expressions thanks to diabolic from EFNet #regex // match hyperlinks with emails $regex = '%(?]*href="(?:mailto:)?([^@"]+@[^@"]+)"[^>]*>(.+?)<\\/a>(?!\\[/nohide\\])%i'; $content = preg_replace_callback($regex, "mh_replace_hyperlink", $content); // match emails $regex = '%\\b([\\w.+-]+@[a-z\\d.-]+\\.[a-z]{2,6})\\b(?!\\s*\\[\\/nohide\\]|(?:(?!]*>).)*<\\/a>)%iU'; $content = preg_replace_callback($regex, "mh_replace", $content); // remove the nohides $content = preg_replace('/\[\/?nohide\]/i','',$content); return $content; } // replace the hyperlinked emails i.e. this or that function mh_replace_hyperlink($matches) { global $recaptcha_opt; // get the url, the part inside the href. this is the email of course $url = recaptcha_mailhide_url($recaptcha_opt['mailhide_pub'], $recaptcha_opt['mailhide_priv'], $matches[1]); // construct a new hyperlink with the url hidden but the link text the same $html = "" . $matches[2] . ""; // style it $html = '' . $html . ""; return $html; } // replace the plain text emails i.e. haha@lol.com function mh_replace($matches) { global $recaptcha_opt; // fine plain text emails and hide them $html = recaptcha_mailhide_html($recaptcha_opt['mailhide_pub'], $recaptcha_opt['mailhide_priv'], $matches[1]); // style it $html = '' . $html . ""; return $html; } // add the filters only if mcrypt is loaded if ($recaptcha_opt['use_mailhide'] && extension_loaded('mcrypt')) { add_filter('the_content', 'mh_insert_email'); // For posts/pages add_filter('get_comment_text', 'mh_insert_email'); // For comments add_filter('the_content_rss', 'mh_insert_email'); // For RSS add_filter('comment_text_rss', 'mh_insert_email'); // For RSS Comments } /* ============================================================================= End MailHide ============================================================================= */ /* ============================================================================= reCAPTCHA - The reCAPTCHA comment spam protection section ============================================================================= */ function recaptcha_wp_hash_comment($id) { global $recaptcha_opt; if (function_exists('wp_hash')) return wp_hash(RECAPTCHA_WP_HASH_COMMENT . $id); else return md5(RECAPTCHA_WP_HASH_COMMENT . $recaptcha_opt['privkey'] . $id); } function recaptcha_wp_get_html ($recaptcha_error, $use_ssl=false) { global $recaptcha_opt; return recaptcha_get_html($recaptcha_opt['pubkey'], $recaptcha_error, $use_ssl, $recaptcha_opt['re_xhtml']); } /** * Embeds the reCAPTCHA widget into the comment form. * */ function recaptcha_comment_form() { global $user_ID, $recaptcha_opt; // don't show reCAPTCHA to admins if (($recaptcha_opt['re_noadmins'] && current_user_can('level_10')) || !$recaptcha_opt['re_comments']) return; else { // Did the user fail to match the CAPTCHA? If so, let them know if ($_GET['rerror'] == 'incorrect-captcha-sol') echo "

Incorrect CAPTCHA. Please try again.

"; //modify the comment form for the reCAPTCHA widget $recaptcha_js_opts = << var RecaptchaOptions = { theme : '{$recaptcha_opt['re_theme']}', lang : '{$recaptcha_opt['re_lang']}' , tabindex : {$recaptcha_opt['re_tabindex']} }; OPTS; $comment_string = << COMMENT_FORM; if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") $use_ssl = true; else $use_ssl = false; echo $recaptcha_js_opts . recaptcha_wp_get_html($_GET['rerror'], $use_ssl) . $comment_string; } } add_action('comment_form', 'recaptcha_comment_form'); function recaptcha_wp_show_captcha_for_comment() { global $user_ID; return true; } $recaptcha_saved_error = ''; /** * Checks if the reCAPTCHA guess was correct and sets an error session variable if not * @param array $comment_data * @return array $comment_data */ function recaptcha_wp_check_comment($comment_data) { global $user_ID, $recaptcha_opt; global $recaptcha_saved_error; if ((current_user_can('level_10') && $recaptcha_opt['re_noadmins']) || !$recaptcha_opt['re_comments']) return $comment_data; if (recaptcha_wp_show_captcha_for_comment()) { if ( $comment_data['comment_type'] == '' ) { // Do not check trackbacks/pingbacks $challenge = $_POST['recaptcha_challenge_field']; $response = $_POST['recaptcha_response_field']; $recaptcha_response = recaptcha_check_answer ($recaptcha_opt ['privkey'], $_SERVER['REMOTE_ADDR'], $challenge, $response); if ($recaptcha_response->is_valid) return $comment_data; else { $recaptcha_saved_error = $recaptcha_response->error; add_filter('pre_comment_approved', create_function('$a', 'return \'spam\';')); return $comment_data; } } } return $comment_data; } /* * If the reCAPTCHA guess was incorrect from recaptcha_wp_check_comment, then redirect back to the comment form * @param string $location * @param OBJECT $comment * @return string $location */ function recaptcha_wp_relative_redirect($location, $comment) { global $recaptcha_saved_error; if($recaptcha_saved_error != '') { //replace the '#comment-' chars on the end of $location with '#commentform'. $location = substr($location, 0,strrpos($location, '#')) . ((strrpos($location, "?") === false) ? "?" : "&") . 'rcommentid=' . $comment->comment_ID . '&rerror=' . $recaptcha_saved_error . '&rchash=' . recaptcha_wp_hash_comment ($comment->comment_ID) . '#commentform'; } return $location; } /* * If the reCAPTCHA guess was incorrect from recaptcha_wp_check_comment, then insert their saved comment text * back in the comment form. * @param boolean $approved * @return boolean $approved */ function recaptcha_wp_saved_comment() { if (!is_single() && !is_page()) return; if ($_GET['rcommentid'] && $_GET['rchash'] == recaptcha_wp_hash_comment ($_GET['rcommentid'])) { $comment = get_comment($_GET['rcommentid']); echo ""; wp_delete_comment($comment->comment_ID); } } function recaptcha_wp_blog_domain () { $uri = parse_url(get_settings('siteurl')); return $uri['host']; } add_filter('wp_head', 'recaptcha_wp_saved_comment',0); add_filter('preprocess_comment', 'recaptcha_wp_check_comment',0); add_filter('comment_post_redirect', 'recaptcha_wp_relative_redirect',0,2); function recaptcha_wp_add_options_to_admin() { if (function_exists('is_site_admin')) { if (is_site_admin()) { add_submenu_page('wpmu-admin.php', 'reCAPTCHA', 'reCAPTCHA', 'manage_options', __FILE__, 'recaptcha_wp_options_subpanel'); add_options_page('reCAPTCHA', 'reCAPTCHA', 'manage_options', __FILE__, 'recaptcha_wp_options_subpanel'); } } else { add_options_page('reCAPTCHA', 'reCAPTCHA', 'manage_options', __FILE__, 'recaptcha_wp_options_subpanel'); } } function recaptcha_wp_options_subpanel() { $optionarray_def = array( 'pubkey' => '', 'privkey' => '', 'use_mailhide' => '', 're_noadmins' => '', 'mh_noadmins' => '', 'mailhide_pub' => '', 'mailhide_priv' => '', 're_theme' => 'red', 're_theme_reg' => 'red', 're_lang' => 'en', 're_tabindex' => '5', 're_comments' => '1', 're_registration' => '1', 're_xhtml' => '0', ); add_option('recaptcha', $optionarray_def, 'reCAPTCHA Options'); /* Check form submission and update options if no error occurred */ if (isset($_POST['submit'])) { $optionarray_update = array ( 'pubkey' => $_POST['recaptcha_opt_pubkey'], 'privkey' => $_POST['recaptcha_opt_privkey'], 'use_mailhide' => $_POST['use_mailhide'], 're_noadmins' => $_POST['re_noadmins'], 'mh_noadmins' => $_POST['mh_noadmins'], 'mailhide_pub' => $_POST['mailhide_pub'], 'mailhide_priv' => $_POST['mailhide_priv'], 're_theme' => $_POST['re_theme'], 're_theme_reg' => $_POST['re_theme_reg'], 're_lang' => $_POST['re_lang'], 're_tabindex' => $_POST['re_tabindex'], 're_comments' => $_POST['re_comments'], 're_registration' => $_POST['re_registration'], 're_xhtml' => $_POST['re_xhtml'], ); update_option('recaptcha', $optionarray_update); } /* Get options */ $optionarray_def = get_option('recaptcha'); /* ============================================================================= reCAPTCHA ============================================================================= */ ?>

reCAPTCHA Options

reCAPTCHA asks commenters to read two words from a book. One of these words proves that they are a human, not a computer. The other word is a word that a computer couldn't read. Because the user is known to be a human, the reading of that word is probably correct. So you don't get comment spam, and the world gets books digitized. Everybody wins! For details, visit the reCAPTCHA website.

NOTE: If you are using some form of Cache plugin you will probably need to flush/clear your cache for changes to take effect.

reCAPTCHA Key

reCAPTCHA requires an API key, consisting of a "public" and a "private" key. You can sign up for a free reCAPTCHA key.






/>


/>




/>
/>

MailHide Options

MailHide hides email addresses like so: supp...@recaptcha.net. MailHide also requires a public and private key which you can generate using the key generation service.

/>



You can style the hidden emails with the emailrecaptcha CSS class in the recaptcha.css stylesheet in recaptcha's plugin folder.

You can bypass email hiding by enclosing the email with [nohide][/nohide] tags.

/>

reCAPTCHA is not active You must enter your reCAPTCHA API key for it to work

"; } add_action('admin_footer', 'recaptcha_warning'); return; } // If the mcrypt PHP module isn't loaded then display an alert if (($recaptcha_opt['use_mailhide'] && !extension_loaded('mcrypt')) && !isset($_POST['submit'])) { function mcrypt_warning() { $path = plugin_basename(__FILE__); $top = 0; if ($wp_version <= 2.5) $top = 12.7; else $top = 7; echo "

MailHide is not active You must have the mcrypt module loaded for it to work

"; } add_action('admin_footer', 'mcrypt_warning'); return; } // If MailHide is enabled but no keys have been entered if ($recaptcha_opt['use_mailhide'] && !($recaptcha_opt['mailhide_pub'] && $recaptcha_opt['mailhide_pub']) && !isset($_POST['submit'])) { function mailhide_warning() { $path = plugin_basename(__FILE__); $top = 0; if ($wp_version <= 2.5) $top = 12.7; else $top = 7; echo "

MailHide is not active You must enter your MailHide API keys for it to work

"; } add_action('admin_footer', 'mailhide_warning'); return; } /* ============================================================================= End Apply the admin menu ============================================================================= */ ?>