Currently browsing category: wordpress

New Twitter API v1.1 and getting user status timeline

So I recently had problem with twitter api as they dropped down v1 and now Twitter API v1.1 required authentication for all request to twitter apis. I had implemented twitter api in some of the blogs I developed and this will effect all of them. So Below is code based on WordPress but can be implemented on other php applications as well. You will need to create your app and get consumer key and consumer secret. What this script does is get usertimeline from twitter and saved in cache folder of template which can be later used to display user tweets. I hope it will be useful for you guys who are affected by this version change.

<?php
$twitter_json_file = get_theme_root() . '/' . get_template() . '/cache/twitter.json';
if ((is_file($twitter_json_file) and filemtime($twitter_json_file) < time() - 3600) OR !is_file($twitter_json_file)) {
  $consumer_key = 'YOUR-CONSUMER-KEY';
  $consumer_secret = 'YOUR-CONSUMER-SECRET';
  $screen_name = 'yalamber';
  $authorization_key = $consumer_key . ':' . $consumer_secret;
  $authorization_header = base64_encode($authorization_key);
  $response = wp_remote_post('https://api.twitter.com/oauth2/token', array(
    'method' => 'POST',
    'httpversion' => '1.1',
    'headers' => array('Authorization' => 'Basic ' . $authorization_header, 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'),
    'body' => array('grant_type' => 'client_credentials')
   ));
   if (isset($response['body'])) {
     $token_body = json_decode($response['body']);
     if (isset($token_body->access_token)) {
      $access_token = $token_body->access_token;
      $response = wp_remote_get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" .$screen_name. "&count=5", array(
        'httpversion' => '1.1',
        'headers' => array('Authorization' => 'Bearer '. $access_token)
      ));
      if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
      }
      else{
        file_put_contents($twitter_json_file, $response['body']);
      }
    }
  }
}
$results = file_get_contents($twitter_json_file);
$tweets = json_decode($results, true);

Contact form 7 useful hook

 function wpcf7_mail_users_custom($cfdata) {
    $formtitle = $cfdata->title;
    $formdata = $cfdata->posted_data;
    //logic code here
  }

  add_action('wpcf7_mail_sent', 'wpcf7_mail_users_custom', 1);

Get the latest version number of WordPress Using API

I am going to show you how to check for the latest version of WordPress using API provided by WordPress.
Below is the code to get the latest version of WordPress from api at http://api.wordpress.org/core/version-check/1.6/ [I did not know it existed. but there are several API provided by WordPress, go to http://codex.wordpress.org/WordPress.org_API]
You will have to create a cache folder which will save the response in a file so that we do not ping the WordPress server often. You can use this code in your website to show latest version number as I have done in wpambulance.com

<?php
function getData($url) {
	if(is_callable('curl_init')){
	  $ch = curl_init();
	  $timeout = 5;
	  curl_setopt($ch, CURLOPT_URL, $url);
	  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	  $data = curl_exec($ch);
	  curl_close($ch);
	  return $data;
	}else{
		return file_get_contents($url);
	}
}
//check for latest WordPress version
$apiUrl = 'http://api.wordpress.org/core/version-check/1.6/';
$apiFile = 'cache/api.json';
if((is_file($apiFile) and filemtime($apiFile)< time()-3600) OR !is_file($apiFile))
{
  $apiContent = getData($apiUrl);
  if($apiContent!=''){
    file_put_contents($apiFile, $apiContent);
  }
}
$apiContent = file_get_contents($apiFile);
$apiReturn = unserialize($apiContent);
$current_version = $apiReturn['offers'][0]['current'];
?>

SEO friendly timthumb on wordpress

Many of you love using timthumb in your wordpress theme but don’t use it because it does not generates seo friendly urls for images. I was asked by a client to make timthumb urls seo friendly and here is how I did that.

First open your wordpress site .htaccess file. Generally it is located in root of your wordpress installation.

It should look like this:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Now create new folder in your root call it let’s say images and place cache folder and timthumb in there then edit your .htaccess file as below

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(images) [NC]
RewriteRule . /index.php [L]
RewriteRule ^images/(.*)x(.*)/r/(.*) images/timthumb.php?src=http://example.com/$3&w=$1&h=$2&zc=1&q=100&a=tl


# END WordPress

Replace example.com with you site name:
Now upload htaccess file and where you need to use timthumb do as below

<?php
if (has_post_thumbnail()):
  $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
  echo '<a href="'.get_permalink().'"><img alt="'.get_the_title().'" src="'.get_bloginfo('url').'/images/715x440/r/'.str_replace(array('http://example.com/', 'http://www.example.com/'), array('',''), $thumbnail[0]).'" width="715" height="440" /></a>';
endif;
?>

That should do the trick for making timthumb url seo friendly.

reportisp.com

Reportisp.com is a site to review and rate your isp. We know you have lots of complaints about your isp and all. their support sucks. Try reviewing your isp in this site and lets hope some ISP’s will listen.
link: www.reportisp.com

changing timezone and locale for time in wordpress

In a recent project i had to change the time displayed in the wordpress posts to spanish language. I think there are other better ways than mine to change the time and locale in wordpress but what i came up with quickly was to set the locale to spanish and then use strftime function to change the time to spanish language.

  1. set the locale in the theme header.php file.
    setlocale(LC_ALL, ‘es_ES.UTF8′); //at the very top of the theme header file.
  2. using strftime function to change the time in the wordpress loop function.
    <?php echo strftime(“%A, %d”,strtotime(get_the_time(‘d M Y’)));?> de <?php echo strftime(“%B”,strtotime(get_the_time(‘d M Y’)));?> de <?php echo strftime(“%Y”, strtotime(get_the_time(‘d M Y’)));?>

This was how i set the locale for time to spanish in wordpress. if you have better ways out please comment.

powered by WordPress | theme by yalamber S.