Skip to content

Instantly share code, notes, and snippets.

@rohmann
Last active December 21, 2015 04:59
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 rohmann/6253270 to your computer and use it in GitHub Desktop.
Save rohmann/6253270 to your computer and use it in GitHub Desktop.
Simple spam protection for Wordpress. Base64 encode with PHP, then decode with Javascript. Was in a repo, but just moving to gist as it's simple enough.
<?php
//Create a shortcode that encrypts the given text and adds a tag around the text that will lated be used to find the text on the client side
add_shortcode( 'nospam', function ( $atts, $content = null ) {
return '<em class="tsp-nospam">'.base64_encode($content).'</em>';
});
add_action('wp_head', function(){ ?>
<script>/* js base64 decode modified/condensed from http://www.webtoolkit.info/javascript-base64.html*/function wptspdecode(a){var b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",c="",d,e,f,g,h,i,j,k=0,l="",m=c1=c2=0;a=a.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(k<a.length){g=b.indexOf(a.charAt(k++));h=b.indexOf(a.charAt(k++));i=b.indexOf(a.charAt(k++));j=b.indexOf(a.charAt(k++));d=g<<2|h>>4;e=(h&15)<<4|i>>2;f=(i&3)<<6|j;c+=String.fromCharCode(d);i!=64&&(c+=String.fromCharCode(e));j!=64&&(c+=String.fromCharCode(f))}k=0;while(k<c.length){m=c.charCodeAt(k);if(m<128){l+=String.fromCharCode(m);k++}else if(m>191&&m<224){c2=c.charCodeAt(k+1);l+=String.fromCharCode((m&31)<<6|c2&63);k+=2}else{c2=c.charCodeAt(k+1);c3=c.charCodeAt(k+2);l+=String.fromCharCode((m&15)<<12|(c2&63)<<6|c3&63);k+=3}}return l}
jQuery(document).ready(function(){jQuery("em.tsp-nospam").removeClass("tsp-nospam").replaceWith(function(){return wptspdecode(jQuery(this).html())})});</script>
<style>.tsp-nospam{display:none;}</style>
<?php });
@rohmann
Copy link
Author

rohmann commented Sep 10, 2013

This can be placed in at theme's functions.php or within a plugin.

For quick implementation without coding, use these steps:

  1. Download the PHP file by click clicking the raw icon above (see image) and chosing "Save link as"
  2. Upload that file to the wp-content/mu-plugins folder
    alt text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment