Tuesday, December 22, 2009
No-Plugin Twitter + WordPress

While one of the greatest strengths of WordPress as a web design tool is the wide range of open-source plug-ins it offers, I often find them quite difficult to customize. So when I wanted to link my Twitter to this website, I searched for a way to do it without plug-ins. I found a beautiful snippet of back-end code at yoast.com that allows me to access the Twitter API, snag the latest entry, and return an array containing its attributes. God bless yoast. I hope he doesn’t mind me posting this here.

The code uses PHP and its Snoopy object to crack into the API using JSON:

1
2
3
4
5
6
7
8
9
10
11
<?php
require_once(ABSPATH . 'wp-includes/class-snoopy.php');
$snoopy = new Snoopy;
$snoopy->fetch("http://twitter.com/statuses/user_timeline/ysquared86.json?count=1");
$twitterdata = json_decode($snoopy->results,true);
$pattern = '/\@([a-zA-Z]+)/';
$replace = '<a href="http://twitter.com/'.strtolower('\1').'">@\1</a>';
$output = preg_replace($pattern,$replace,$twitterdata[0]["text"]);
$output = make_clickable($output);
echo "\"".$output."\"";
?>

Line 2 and 3 includes the Snoopy files and creates a new Snoopy object to fetch your data. Line 4 specifies the location of the data. ysquared86 is my Twitter username – replace it with yours. The ?count=1 grabs the latest tweet. Line 5 decodes the data using JSON and creates an array named $twitterdata. Lines 6 through 8 finds any other usernames in the entries and creates a link to their twitter page (e.g. @ysquared86). Line 9 uses WordPress’ make_clickable function to retain any links, and Line 10 finally prints the entry onto the page.

Another important attribute you might want to display is the timestamp, but yoast did not cover the topic in his entry, and I had to do an extensive google search to figure it out on my own. Most WP plug-ins for Twitter do this successfully, but again, I’d rather know what my code is doing. It took me forever to find that, as the $twitterdata[0]["text"] was the body of the twitter entry, “created_at” returns the timestamp attribute. However, it turns out, this timestamp does not follow any conventional format, which meant I had to use PHP’s substr function to make it look nice:

1
2
3
4
5
6
7
8
9
<?php
$timestamp = $twitterdata[0]["created_at"];
$day = substr($timestamp, 0, 3);
$month = substr($timestamp, 4, 3);
$date = substr($timestamp, 8, 2);
$time = substr($timestamp, 11, 8);
$year = substr($timestamp, 26, 4);
echo "Posted at ".$time." on ".$day.", ".$month." ".$date." ".$year;                       
?>

So there it is. I wanted to be able to CSS my way around the positioning, and having these PHP chops worked perfectly.

Friday, December 11, 2009
Tip that point.

Youd be crazy not to read it

I don’t read very much anymore, at least not nearly as much as when I was forced to. But lately I have been lucky choosing amazing books. To me, a good book not only is educational but must inspire the reader to reassess a good chunk of his/her world, and my recent readings, including Richard Dawkins‘ illuminating the God Delusion – by far the strongest argument for atheism I had ever heard -  have been quite successful at that.

I picked up the Tipping Point by Malcolm Gladwell through a recommendation. I have a habit of skimming through the first few pages before starting which, until the Tipping Point, has dissuaded me from starting at all (it is undoubtedly the reason behind my decreased bibliophilia). This book managed to grip me through the mere chapter headings. As someone who has worked in the marketing department and a web designer who is perpetually wallowed in the riddle of how to reach the people, I could not think of a better book for me to have read.

Gladwell dissects the phenomenon of social epidemics, ranging from fashion trends and diseases to TV shows and Paul Revere. He divides the key players in the “tipping” of these epidemics into three categories: connectors, salesmen, and mavens. All of us know at least one person in our lives who knows everyone, or convinces others naturally, or seems to know everything there is to know. His examples in the book surely are extreme cases, but I enjoyed the systematic approach with which he tried to put a finger on the epicenters of social epidemics. He emphasizes how small and modest changes can “tip” a concept to permeate a society – who could have thought the seemingly simplest shows on TV required more thought than any other? Who would have imagined…but I don’t want to spoil the book for you. On a scale from a Jimmy Fallon joke to Maxim magazine, this book is Hemingway.

Wednesday, December 9, 2009
Simple Chicken & Rice Bake

CIMG3607

I found this in a short cookbook and gave it a try the other night. It turned out to be delightful – and so simple. I do enjoy a fair share of ethnic food but American cooking does keep the processes as straightforward as possible, with the minimal ingredients. Here’s the recipe:

  • 1 1/2 lb. chicken
  • 8 bacon slices
  • Olive oil for stir-frying
  • 1 1/4 cups rice
  • 1 onion, chopped
  • 2 garlic cloves, crushed
  • 1 tsp. ground turmeric
  • Zest and juice of 1/2 lemon
  • 2 cups hot chicken stock
  • 1 tbsp. chopped cilantro
  • Salt and black pepper to taste
  1. Preheat the oven to 350 degrees Fahrenheit.
  2. Wrap chicken pieces with a bacon slice and secure in place with a toothpick.
  3. Oil a nonstick pan, and sear the chicken for 5 minutes until browned. Remove and set aside.
  4. Add the rice to the pan and stir for 1 minute. Add onion, garlic, turmeric, lemon zest, stock, and salt and pepper to taste.
  5. Transfer the mixture to a casserole pan, and arrange the chicken pieces over the rice.
  6. Cover with foil and lid. Bake for 50 minutes and garnish with cilantro.
  7. Tangy Yogurt Sauce (optional): 2/3 cup yogurt + 1 tsp lemon zest + 1 crushed garlic clove + 2 tsp lemon juice + parsley, salt and pepper to taste.

Not as impressive as your everyday Mapo Tofu or Pernil, but sometimes simple works.