The best wordpress plugin for trending content at your website or blog

Often, when you get started writing some content for your website or a personal blog, you don’t know what is a trend at the moment. To make research “what is hot today”, is boring or you don’t have time for this, but content which is not popular is less attractive for users. For that purpose, I made a WordPress plugin, Popular on Web, that suggests what to write, based on trending searches from google search.

Popular on Web plugin is very useful for websites that want to stay with the most searched and

popular content on the web. Also, this plugin is useful for “niche blogs”, which always must have popular content.

popular-on-web-1

How does it work?

This is a simple plugin that fetches data from Google Trends and shows the latest trending searches on the web-based on google search results, grouped by country regions. You can choose trending searches from 47 different regions.

popular-on-web-3

When you change region, Popular on Web makes an ajax request that returns JSON content for new data for the chosen region and replaces actual content with new.

Javascript

$('#pow-choose-region').change(function(){
			$('#pow-region').html( $('#pow-choose-region option:selected').text() );
			var region_id = $(this).val();
			var nonce = $(this).attr('data-nonce');
	        $.ajax({
	            type: 'POST',
	            dataType: 'json',
	            url: ajaxurl,
	            data: { 'action' : 'getpow', 'nonce' : nonce, 'region_id' : region_id },
	            success:function( resp ){
	            	var content = '';
	            	$('#pow-content').html('<p class="text-center" style="margin-top:50px">Please wait...</p>');
	            	if( resp.length > 0 )
	            	{
	            		for( var i=0; i<resp.length; i++)
	            		{
	            			content += '<div class="row"><div class="col-xs-4 text-center"><a href="'+resp[i].url+'" target="_blank"><img src="'+resp[i].picture+'" alt="'+resp[i].title+'" class="pow-thumb"></a></div><div class="col-xs-8"><a href="'+resp[i].url+'" target="_blank">'+resp[i].title+'</a><div class="snippet">'+resp[i].snippet+'</div></div></div>';
	            		}
	            	}
	            	$('#pow-content').html( content );
	            }
	        });		
});

PHP code

public function get_popular_searches( $id = 'p1' ) {
		$items = apc_fetch( 'pow-items-' . $id );
		if( false === $items )
		{
			$url = 'https://www.google.com/trends/hottrends/atom/feed?pn=' . $id;
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
			curl_setopt($ch, CURLOPT_SSLVERSION , 1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_URL , $url );
			$response = curl_exec($ch);
			if($response === false)
			{
			    exit( 'Curl error: ' . curl_error($ch) );
			}
			curl_close($ch);
			$items = str_replace(array('<ht:','</ht:'),array('<','</'), $response);
			apc_store( 'pow-items-' . $id, $items, 600 );
      }

      return simplexml_load_string($items);
}

As you can see, the server-side function returns a SimpleXML object that is a built-in function in PHP.

Important

Your server must have installed the APC extension, to make this plugin work properly.

While waiting for approval from WordPress’s official plugin repository, you can find and test the entire code at my github.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: