Display your site’s current PageRank

If you have a need to add your site’s current PageRank to a page or post (for example, you may have a page for the media/PR on which you may wish to highlight it),  the following piece of code, added to your theme’s functions.php file, will achieve just that.

All you have to add in a post or page is add [pagerank] wherever you wish the PageRank to appear.

To achieve this, the code uses the site Prapi.net to retrieve the information. The result is cached for 24 hours so as not to inflict on site performance.

You can change the caching time by modifying the number 86400 (which represents the number of seconds to cache for). If you wish to change the name of the shortcode, then simply change that on the final line.

<?php
function pagerank_sc( $paras = '', $content = '' ) {
$cache_key = 'functions_pagerank';
$cache = get_transient( $cache_key );
if ( !$cache ) {
$fileout = wp_remote_get( 'http://prapi.net/pr.php?url=' . <code>get_site_url() </code>. '&amp;f=text' );
if ( isset( $fileout[ 'body' ] ) ) {
$content = $fileout[ 'body' ];
set_transient( $cache_key, $content, 86400 );
}
} else {
$content = $cache;
}
return $content;
}
add_shortcode( 'pagerank', 'pagerank_sc' );
?>
view raw functions.php hosted with ❤ by GitHub

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from David Artiss

Subscribe now to keep reading and get access to the full archive.

Continue reading