Implementing ReCaptcha into WordPress contact form

Posted by Freshface on September 09, 2011 in Fast Tricks, Wordpress tagged with

ReCaptcha is a powerful tool to limitate the huge amount of spam. Google recently take over this service and made it really easy to use. But we made a small wrapper around the class and created easiest captcha in the world :D

Follow these steps:

1.) Create your ReCaptcha account here

2.) Download the PHP recaptchalib.php here

3.) Create a new php file called for example recaptcha_wrapper.php and insert this code :

require_once 'recaptchalib.php';

# KEYS
$publicKey = '';
$privateKey = '';
$enableRecaptcha = true;
# print the reCaptcha element
function recaptcha_print() {
	if( !$enableRecaptcha ) return;
	global $publicKey;
	echo recaptcha_get_html($publicKey, $error);
}

# check the reCaptcha response.
# return TRUE / FALSE
function recaptcha_check() {
	if( !$enableRecaptcha ) return true;
	global $privateKey;
	# was there a reCAPTCHA response?
	if ($_POST["recaptcha_response_field"]) {
	        $resp = recaptcha_check_answer ($privateKey,
	                                        $_SERVER["REMOTE_ADDR"],
	                                        $_POST["recaptcha_challenge_field"],
	                                        $_POST["recaptcha_response_field"]);

	        if ($resp->is_valid) {
	                return true;
	        } else {
	                return false;
	        }
	}
}

4.) Using:

Print ReCaptcha: call function recaptcha_print();

Validating ReCaptcha: call function recaptcha_check(); // return true or false;

Continue ReadingView Comments (1)

How to get rid of that ‘Read More’ jump in WordPress

Posted by Freshface on March 03, 2011 in Fast Tricks, FreshTips, Wordpress tagged with

If you call
the_content()
function in WordPress, it creates the default ‘Read More’ button, similar to this:

<a href=”yoursite.com/your-article/#more-ID“>

So let’s say you want to get rid of this junky ‘Read More’ jump. How would you do that? WordPress doesn’t have any option for removing this link in the wp-admin, so I wrote a simple function you can use in your functions.php:

// adds an action hook, which calls function after the content is processed.
add_action('the_content','remove_sharp_in_more_link');

// our steve-o-magic function, parameter $content is the content of your post/page
function remove_sharp_in_more_link($content)
{
    // here we use regular expression to remove the more ID
    // the [0-9] means every number, and the + means 1 or more numbers in a row ("5" or "55" or "5555")
    return ereg_replace('#more-[0-9]+',' ',$content);
}
Continue ReadingView Comments (4)

How to catch an output of a wordpress echo function

Posted by bOObs.lover on February 02, 2011 in Fast Tricks, FreshTips, Wordpress tagged with

Sometimes you need to know data from function, which only prints them. This function is not returning the data, and its pretty anoying. For example it could be function called:

the_author_posts_link();

you simple cant do this:

$variable = the_author_posts_link();

Solution:

/* ob_start() -> all echo commands are not printed, but collected into buffer */

ob_start(); // enable collecting into buffer
the_author_posts_link(); // call the function and catch output into buffer
$variable = ob_get_contents(); // read buffer
ob_end_clean(); // end collecting into buffer and flush the buffer content
Continue ReadingView Comments (1)

WordPress get category ID

Posted by Freshface on February 02, 2011 in Fast Tricks, FreshTips, Wordpress tagged with

Sometimes you will need ID of the actual category. It will be lil bit complicated, if you are in single post. So I wrote a simple function, which help you to get wordpress category ID.

(more…)

Continue ReadingView Comments (1)

Blackhat SEO – Part 1 – Content Scraping

Posted by bOObs.lover on November 11, 2010 in Blackhat SEO, Blackhat SEO Basics tagged with

Hi Guys, I’m glad that I can announce a new Blackhat SEO series. In this first part we are going to talk about content scraping. In this series, I will be using C# and PHP and MySQL. All you need are basics of these languages (I’m not good at C#, either).

Main difference between Blackhat and Whitehat

Content scraping is absolutely necessary for Blackhat SEO projects. Because if you do Whitehat SEO, you basically create hand-written unique content. All these handwritten stuff costs money (you have to hire guys or write it by your own). On the other hand, Blackhat SEO is based on the content generation and quantity. Most of us won’t be able to write a content generator which will be able to produce results as good as a real person. So there goes quantity ( we are replacing quality with quantity). For generating a “new” content we will need some kind of “old” content. And here is the true value of content scraping.

C# class for scraping a page

OK, what comes to your mind when you hear “scrape a page”, what do you imagine? In my mind comes: open and read page, get all links ( outcoming / inner links ), get all valuable texts ( basically paragraphs which are longer than 200 – 300 letters ). So I’ve created this class in C# which can do all these things.

1. Reading a web page

public string openPage(string paramUrl)
{
 // edit the page url
 this.privUrl = paramUrl;  // save the url into private class variable
 this.privPureUrl = paramUrl.Replace("http://www.", "").Replace("www.", ""); // eg: from http://www.google.com -> google.com
 //if (this.privPureUrl.IndexOf('/') != -1) this.privPureUrl= this.privPureUrl.Substring(0, this.privPureUrl.IndexOf('/')); // google.com/ -> google.com

 if (paramUrl == "") return this.privPage = "";

 Uri uri = new Uri(paramUrl);
 WebRequest req = WebRequest.Create(uri);
 try  // in case of error (e.g. connection timeout, bad url and other)
 {
  WebResponse resp = req.GetResponse();
  Stream stream = resp.GetResponseStream();
  StreamReader sr = new StreamReader(stream);
  this.privPage = sr.ReadToEnd();
 }
 catch // return blank page
 {
  this.privPage = "";
 }

 return (this.privPage);
}

The code above gets the link as a parameter. For example http://www.google.com. We strip from the link the “http://www.” or just “www.” part, so we have “google.com” which we save into private class variable (this.privPureUrl).

Then we open the page and read the whole page into a string (the code inside the “try” block). If this process fails, we go to the catch block, where we assign open page as an empty string. And in the end, we return the opened page as a string.

2. Get all links from a webpage

We have to make one assumption: All links starts with href=” or href=’

So we can split the whole page into an array, divided by href=” or href=’. For this purpose is good using the regular expressions. The C# code is here:

public List<string> getLinks()
{
 // start from body tag
 int start_pos = this.privPage.IndexOf("<body>");
 if (start_pos == -1) start_pos = 1;
 string page_content = this.privPage.Substring(start_pos);

 // split the content
 Regex replace_href = new Regex("href=['|\"]");
 string[] links = replace_href .Split(page_content);

 List<string> links_to_return = new List<string&gt()
 int counter = 0;
 for(int i = 1; i < links.Length; i++)
 {
  int simple_quote = links[i].IndexOf('\'');
  int double_quote = links[i].IndexOf('"');

  if (simple_quote < double_quote && simple_quote != -1) double_quote = simple_quote;
  links_to_return.add(links[i].Substring(0, double_quote));
 }
 return links_to_return;
}

So this function basically divides our content into parts. The divider is href=" or href='. We will go through this array and search for first quotes (single or double) which ends the href attribute. Then we easily substring the value and we have a single link.

You can upgrade this function to return only outgoing links, or only inner links, or whatever you want.

3. Get all the valuable content from a website

The valuable website content, ideal to scrape, is located in <p> .... </p> ---> paragraphs. We will go through all paragraphs, and remove everything except letters, numbers and "'!?..

public List<string> getParagraphs()
{
 // split paragraphs
 string[] pars = Regex.Split(this.privPage, "<p");

 // go through every paragraph
 for (int i = 0; i < pars.Length; i++)
 {
  // save actual browsed paragraph into string
  string par = pars[i];
  // end <
  int p_end = par.IndexOf(">") + 1;
  int p_close = par.IndexOf("</p>", p_end);
  if (p_end == -1 || p_close == -1) continue;

  par = par.Substring(p_end, p_close - p_end);
  if (par.Length < this.privMinLenght) continue;

  // filter all things except a-z, 0-9, .?!
  par = Regex.Replace(par, "/&[^;]{1,7};/", "");
  par = Regex.Replace(par, "/[\x00-AZ-az-\xFF]/", "");
  par = Regex.Replace(par, "/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\xFF]/", "");
  par = this.StripTagsCharArray(par);//Regex.Replace(par, "<.*?>","");

  //add paragraph into our paragraphs store
  this.pageParagraphs.Add(par);
 }
 return this.pageParagraphs;
}

Conclusion

Today we have learned how to open a webpage, scrape valuable content and read all links. In another words, the very basics of Blackhat SEO. In the next episodes of this series, I will write about content generation, MadLib sites, linkspamming and other Blackhat SEO techniques.

This series is meant for educational purposes only - if you want to be a Jedi, you also need to know the dark side.

Downloads

C# Content Scraper Class (download and run)

Continue ReadingView Comments (7)

Showing Custom post types

Posted by Freshface on August 08, 2010 in Fast Tricks, Wordpress tagged with

If you are hustling with custom post types, you probably hit a snag with showing these custom posts. At almost every site they are talking only about custom queries. But what if you wanna integrate these custom posts into standart wordpress query ? Let me show :)
(more…)

Continue ReadingView Comments (1)

Category Write Panels

Posted by Freshface on August 08, 2010 in Fast Tricks, Wordpress tagged with

When I was developing Showtime theme, I recognize an interesting problem. I had many portfolio templates (2 colums, 3 colums, fullwidth etc.) and I wanna attach them to categories. There are many solutions. For example create an extra admin page, where can user attach templates to the categories. Other solutions were only variations of the first one. But I though there has to be something like category write panels (You know post write panels, dont you ?). I google this all day long, but with no succes. So I decided to made my own script. (more…)

Continue ReadingView Comments (8)