PageRank API and WordPress PageRank Shortcode

PageRank is one of those topics that’s grossly misunderstood by almost all of those that talk about it… mainly because the specifics of Google’s algorithm is a closely guarded secret. What we do know, however, is that links to your webpage from popular websites contributes towards a higher PageRank score. Incoming links to your website from link-farms or unpopular sites contribute very little – if at all. The algorithm also includes other factors such as the size of a page, the number of changes, the time since the page was updated, the text in headlines and the text in hyperlinked anchor texts. Think of it as the ‘Social Matrix’ of the digital world.

This post will show you how to include the PageRank of a website on any page. The code also provides for a PageRank ‘badge’ to be rendered on your site..

The Google toolbar generates a page rank score for each page you visit (and tracks what you visit to enhance the Google PageRank experience). The toolbar generates a score from 1 to 10 – with 10 being the highest and 1 the lowest. Our code will acknowledge PageRank in the same way – from 1 to 10.

I don’t claim to understand PageRank very well. I have a few podcasts that rank around 4 or 5 when we’re engaged with lots of activity but drop fairly rapidly to a 2 or 3 when we’re stagnant. Adding confusion to the ranks, I have a site with nothing on it that consistently ranks a 4 for reasons that baffle me. It seems that anything up to a PageRank of 5 is relatively easily to achieve, but the degree of difficulty in achieving a higher score increases significantly and exponentially.

A high PageRank doesn’t necessarily mean you’ll score well in Google search results. In fact, PageRank is one of well over 200 metrics used to determine how your site or page is placed. On at least a few occasions, Google has stated that PageRank shouldn’t be relied upon as a measure of a site’s success. Google stated on their product forum that they have been “telling people for a long time that they shouldn’t focus on PageRank so much; many site owners seem to think it’s the most important metric for them to track, which is simply not true.” The Google webmaster help forum states that “… we only update the PageRank displayed in Google Toolbar a few times a year; this is our respectful hint for you to worry less about PageRank… PageRank is an easy metric to focus on, but just because it’s easy doesn’t mean it’s useful for you as a site owner.”

Regardless of what Google claims, and despite the fact it’s not that important, PageRank is interesting and important information to have.

I’ve written a small snippet that’ll output your PageRank score in your WordPress post, page or sidebar and cache it locally for future retrieval. It’ll output the absolute basics… a score from 1 to 10. Server side, the PageRank for a particular URL will be cached for 12 hours. Your user IP address is recorded and limited to one request every 15 minutes. Used in WordPress, the rank is cached for 6 hours by default meaning that the data or badge will always be available on your website.

PageRank Shortcode Examples

Using the WordPress shortcode below, you can include your rank into your WordPress website with the shortcode of [pagerank url="http://www.news.com.au"]. Note the full URL, including the http://www – it’s required! The example shortcode will output the single figure PageRank for News.com.au of 7.

If you wanted to include a PageRank badge on your site, you would use the shortcode of [pagerank url="http://www.facebook.com" image="true"]. This would render an image as follows:


Strict Standards: Non-static method GooglePageRankChecker::getRank() should not be called statically in /home/internoe/public_html/subdomains/pagerank/index.php on line 161

You should copy the code below into your theme’s functions.php file or your own shortcode plugin.

// Internoetics PageRank API
function myPageRank($atts) {
extract(shortcode_atts(array(
    'url' =>; '',
    'image' =>; 'false',
    'cache' =>; '43200' // 12 hours (60*60*12)
  ), $atts));
	$hashedURL = md5("$url");
	$myPageRank = 'PageRank' . $image . $hashedURL;
	$cachedposts = get_transient($myPageRank);
	if ($cachedposts !== false) {
	return $cachedposts;
	} else {
   	if($image) $url .= '&image='  . $image;
	$mypr = file_get_contents("http://pagerank.internoetics.com/?u=$url");
	$return = "$mypr";
    set_transient($myPageRank, $return, $cache);
    update_option($myPageRank, $return);

   return $return;
 }
}
add_shortcode('pagerank','myPageRank');

API Limits

  • Only one request per user can be made every 15 minutes.
  • Once obtained, results are cached for 12 hours.
  • Sites are checked against various spam databases. If the site you’re searching for – even if it has a good PR – is first deemed to include Malware, it’ll output X.
  • If your request is denied due IP throttling or other reasons we’ll also output an X. This will change!

PHP Function

Used outside of WordPress, the naked PHP function works in the same way as the shortcode. The results are cached server-side for 12 hours and IP throttling/limiting applies. That means that you cannot request the PageRank for additional domains after you have made your first request.

function myPageRank($url,$image) {
	$url = strtolower($url);
   	if($image == "") $image = "false";
	$mypr = file_get_contents("http://pagerank.internoetics.com/?u=$url&image=$image");
	return $mypr;
}

// Usage:
$url ="http://www.news.com.au";
$image ="true";
echo myPageRank($url,$image);

It would obviously be faster if you cached the result locally for at least 12 hours. PageRank changes only a few times a year so you might even consider caching the result for up to a week or more.

Matt Cutts Video

Here’s an excellent video from the WordPress WordCamp that was held in San Francisco in 2009. It provides an interesting perspective on how Google indexes your website and how PageRank applies. The original video is available from Viddler here. You can read Matt’s presentation on Google Docs here

Support

The code is quite new so it’s prone to errors on our end. If you encounter bugs, please let us know.

First Name:
Your Email Address:
 




Download: PageRank API
Description: PHP PageRank API function and WordPress Shortcode
Author:Marty
Category: PHP code
Date: May 19, 2012



If you liked this article, you may also like:

  1. Convert Currency on your Website or in a WordPress Post (with Shortcode)
  2. Get YouTube Views with PHP or WordPress Shortcode
  3. Skype Status (Text or Image) with WordPress Shortcode
  4. The favicon and WordPress Link Icon Shortcode (and an Online Image to Icon Converter)
  5. What is WordPress Shortcode?
About Marty

is a passionate web developer from Sydney, Australia. He owns about 600 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. Follow Marty on Twitter or Google+.

Speak Your Mind

*