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

Often, when you get start to write some content for your website or personal blog, you don’t know what is trend at the moment. To make research “what is hot today”, is boring or you don’t have a 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 suggest you what to write, based on trending searches from google search.

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

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

popular-on-web-1

How it works?

This is simple plugin that fetch data from Google Trends and showing 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 ajax request that returns json content for new data for chosen region, and replace 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, server-side function returns simplexml object that is built-in function in php.

Important

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

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

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s