Tag Archives: PHP

An API for the Web: Learning YQL

I just read one of the most interesting articles Net.Tuts+ has published in the last few months: An API for the Web: Learning YQL.

Web apps and web services multiply like rabbits. They’re all fun to play with (like rabbits) and fun to integrate into other projects (unlike rabbits). But learning a new API every other day isn’t feasible or fun. And that’s the problem the Yahoo Query Language (YQL) is out to solve.

Think of YQL as the API for the web, the one API to rule them all. It’s not a hard one to learn, so let’s get you up to speed right now!

I couldn’t have said it better myself. YQL is just that: a wrapper for other APIs. It makes it easy to gather data from virtually any API, mash data up if necessary, and bring it into your own application. With generous daily query limits, and no commercial usage restrictions, what’s not to like?

A basic query to collect tweets from two twitter accounts would look like this:

SELECT * FROM twitter.status.timeline.user WHERE id in ('redwall_hp','fantasyfolder')

Very much like an SQL query, no? And there are “tables” to obtain data from many sources, such as Delicious, Netflix, Facebook, Flickr, Github, Last.fm, RSS feeds, etc.. Is there not a table for an API you need to use? You can create one.

Result sets can be returned as XML or JSON.

Developer tools like this and the BOSS search API, which is one of the available tables in YQL, are what I believe will keep Yahoo alive in the coming years.

Twitter List-Powered “Fan Page” Widget

There’s a really neat post over at Tutorialzine on how to build A Twitter List Powered Fan Page.

It’s a little widget, that would go in your sidebar or some similar place, where it would display the Twitter avatars of anyone who wanted to click a button to add themselves to a Twitter List (along with an overall count of the “fans”).

It seems like a neat idea, though you have to do a bit of PHP and JavaScript work to get it up an running. (It would be nice to see a variation made as a WordPress plugin…)

Using the WordPress Uploader in Your Plugin or Theme

WordPress has a nice media uploader dialog that it uses on the editor pages. Now wouldn’t it be nice if you could use it to handle image uploads for part of a plugin or theme you’re writing? Maybe you want to add an easy way to change the logo in a theme? A simple “Upload Image” button would work quite well for that, wouldn’t it?

It’s fairly simple to implement, providing you already have a bit of experience with the WordPress API.

The first step is to prepare your HTML. Put it wherever the code for your admin page is. You want to have a text input for the image URL, and a button that will launch the uploader dialog.

Continue reading →

Modifying The Contextual Help Menu in WordPress

Contextual help menu in WordPressStarting in version 2.7, WordPress has a pull-down “Help” menu in the upper-right corner of the screen, often joined by another menu for configuring display options for the page in question.

By default the “Help” menu doesn’t do much. It gives some useful pointers on the Dashboard and Write screen, but other than that it pretty much just shows links to the Codex and WordPress.org support forums.

What if you wanted to make the “contextual menu” actually…contextual? If you’re a plugin or theme developer, you can add your own helpful information to the menu. It’s as simple as hooking into the contextual_help filter:

function my_contextual_help($text) {
$screen = $_GET['page'];
if ($screen == 'my_plugin') {
$text = "<h5>Need help with this plugin?</h5>";
$text .= "<p>Check out the documentation and support forums for help with this plugin.</p>";
$text .= "<a href=\"http://example.org/docs\">Documentation</a><br /><a href=\"http://example.org/support\">Support forums</a>";
return $text;

add_action('contextual_help', 'my_contextual_help');

The basic idea is to take the default contents of the menu, $text, and replace it with your own content. The $screen variable is used to make sure that the menu is only changed on the plugin’s pages, rather than universally through the Admin.

Showcase Your Images with WebAssist’s PowerGallery

Looking for a PHP photo gallery package that’s super easy to use? Look no further than WebAssist’s PowerGallery. It has a slick interface that far surpasses any I’ve seen in similar scripts. It looks good, it’s clean and simple, and it feels polished, which is more than I can say for the ever-popular Coppermine Photo Gallery.

PowerGallery Main Screen

Continue reading →

Rolling Your Own PHP Framework

Fuel Your Coding has a three-part series worth checking out. It covers how to build your own lightweight PHP framework for your projects.

There are quite a few php frameworks out there. Some huge, some small; Some useful, some not. I often hear developers, even myself, complain about frameworks in php not having or not doing something the way they/we want it too. So my solution? Roll your own framework! This series of articles is going to give you a brief and quick intro on how to do just that. We are going to cover url routing, basic database connection, basic templating and basic rewrite rules for Apache. What we will not cover is ORM development, advanced routing and installation of LAMP (Linux, Apache, MySQL, PHP). Also error checking will be next to none as this is just an example.

It’s a neat tutorial, and in the end you’ll have something useful: an organized basis for future PHP projects. You have an underlying structure that will help you get into building your next application sooner.

Count Your Retweets with the Tweetmeme API

I didn’t know this until recently, but Tweetmeme has an API for displaying the number of times and URL has been retweeted. A quick request to http://api.tweetmeme.com/url_info?url=[the URL] will return an XML, JSON or PHP response. You can pass the full URL as the argument, or a short URL that goes to the same place.

<title>Learning CSS Sprites</title>
<excerpt>Learning CSS Sprites http://bit.ly/3HlYUl</excerpt>

If you want to see a quick example of how you can use the API, have a look at this WP Recipes snippet: Display the number of tweets for each page or post.

Preventing the Caching of Dynamic Functions in WP Super Cache

WP Super Cache is commonly used to speed up WordPress blogs and reduce server load. In essence, it stores static HTML copies of pages on your blog so they will load faster the next time they are accessed. It works fairly well, but with one caveat that may be frustrating at times: The pages don’t change until the cache expires (every 30-60 minutes or so) and the static page is updated. That makes it hard to do some things where you need to process information unique to each user, such as checking HTTP referrer headers and serving ads to visitors coming from search engines.

Fortunately, there is a way around it in some cases. If you have a function in your template, you can do something like this:

<!--mfunc function_name( 'parameter', 'another_parameter' ) -->
 <?php function_name( 'parameter', 'another_parameter' ) ?>

You take a function call and surround it by the “mfunc” comments, in which the first contains a duplicate of the function. This will (somehow) instruct WP Super Cache to allow the function to execute, even in the static cached version of the page.

TimThumb: Automatically Resize Images

TimThumb is a PHP script by Darren Hoyt that can automatically create thumbnails on the fly, caching them for later use. It scales images to the width and height you specify, either keeping the original aspect ratio or cropping the image.

To make use of the script, once installed, you simply put the URL of the TimThumb script in an image tag, and pass arguments for the original image and the scaling options.

<img src="/thumbnails/timthumb.php?src=
/images/an_image.jpg&w=128&h=128&zc=1" alt="">

This would be perfect for generating thumbnails for blog posts, which is, interestingly enough, what the script was originally developed for.

WordPress’s WPDB Class

WordPress has a class, $wpdb, that contains several useful functions for manipulating the database. (The $wpdb object is a global variable that WordPress automatically creates.) This is used throughout the core, and by plugin and theme developers to easily run custom MySQL operations. It even has functions for building and executing prepared statements.

You can find a nice introduction and examples at WP Engineer. Here’s a sample of how you would go about changing the title of a post with a certain ID:

$wpdb->update( $wpdb->posts, array( 'post_title' => $new_post_title ),
 array( 'ID' => $the_post_id ) );

That’s just scratching the surface, but it gives a good idea of how the database functions work.

For even more in-depth reading, there’s a good Codex page on the subject as well.