When you upload images into WordPress, it automatically creates smaller derivative images to serve as thumbnails. You have little control over these derivatives, aside from setting their maximum dimensions. If you upload any non-image media (e.g., a PDF, a video, an MP3, a tarball, etc.), you don’t get any thumbnail, for fairly obvious reasons.
Let’s say you want more control over your image thumbnails, or you would like to have thumbnails attached to your other uploads. You would need to install a plugin like the Custom Thumbnails plugin I just wrote.
What it does
WordPress associates each upload with a post or a page, using the item’s post_parent attribute. This plugin adds a field to the editing screen for your upload, allowing you to associate it with a “Parent Item”. Once thus associated, any time WordPress requests the thumbnail image of the parent item, it will retrieve the thumbnail image you uploaded, instead.

How it works
Writing this plugin took me much farther into the bowels of WordPress than I had reached before. As it turns out, I needed to use four completely undocumented filters to achieve the effect I wanted. More…
Categories: Coding | 9 Comments
Jonathan Brinley | 2008-11-17 11:15
Today I converted Eric Lease Morgan’s c4lj2doaj.cgi into a WordPress plugin.
What is the DOAJ?
The Directory of Open Access Journals is an avenue for readers to discover and access the contents of thousands of open access journals (i.e., journals that don’t charge for access to the full text of their articles).
Publishers of said journals can provide article-level data to the DOAJ, opening those articles up to discovery through the DOAJ interface. One can provide this data to the DOAJ through a form (entering the title, authors, abstract, keywords, etc., for each article, one at a time) or by uploading data that conforms to the DOAJ acticle XML schema.
Enter the DOAJ Export plugin
Eric built a Perl module to grab the necessary info from the Code4Lib Journal’s database and present it in this format. This functionality seemed to belong in a WordPress plugin, so I set out to convert Eric’s script into the plugin before you today.
Since this is just presenting the data for each issue in yet another XML format, similar to all the feeds WordPress already creates, I thought it appropriate to make the data accessible as another feed, using the add_feed function I’ve mentioned before.
By going to an issue of the journal and appending /feed/doaj to the URL, you’ll get the contents of that issue in the DOAJ XML format. E.g., http://journal.code4lib.org/issues/issue4/feed/doaj for the latest issue.
You can download the DOAJ Export plugin from the WordPress Plugin Directory.
Categories: Coding | 1 Comment
Jonathan Brinley | 2008-11-03 16:51
There are many reason’s you might need to create a custom feed in WordPress. You may need a feed in an unusual or non-standard format, or you may want to add or edit the content of your feeds. Setting up a custom feed is pretty easy, but poorly documented.
Everything revolves around the add_feed function. This function takes two arguments: a name for your feed and a function to call to create the feed:
add_feed('myFeed', 'myPlugin_create_feed');
The function from the latter argument will handle all of the work of creating your feed. It will have access to the WordPress loop, so after calling have_posts() and the_post(), you can take advantage of all the template tags available.
When to call add_feed
For add_feed to work, you have to wait until WordPress has completely initialized before you call it. You can use the init action hook to accomplish this at the right time.
add_action('init', 'myPlugin_add_feed');
function myPlugin_add_feed( ) {
add_feed('myFeed', 'myPlugin_create_feed');
}
Changing Rewrite Rules
If you stopped there, WordPress would call myPlugin_create_feed() whenever you go to a page and add the query string feed=myFeed to the end of the URL. Example: http://example.com/?feed=myFeed or http://example.com/?s=foo&feed=myFeed.
If you want to make the URL for your feed look like the other feeds in WordPress (e.g., http://example.com/feed/atom), you have to change the rewrite rules.
To tell WordPress to add new rules, use the generate_rewrite_rules action hook. It will call your function just before it finishes building its rules, with the $wp_rewrite object as an argument. Simply add your new rule to the pre-existing $wp_rewrite->rules array.
function myPlugin_rewrite_rules( $wp_rewrite ) {
$new_rules = array(
'feed/(.+)' => 'index.php?feed='.$wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
You can add this action hook into your myPlugin_add_feedfunction. Finally, you need to tell WordPress to rebuild its rewrite rules now that it has your additions, using $wp_rewrite->flush_rules().
function myPlugin_add_feed( ) {
global $wp_rewrite;
add_feed('myFeed', 'myPlugin_create_feed');
add_action('generate_rewrite_rules', 'myPlugin_rewrite_rules');
$wp_rewrite->flush_rules();
}
Now your customized feed should be accessible at http://example.com/feed/myFeed.
Categories: Coding | 19 Comments
Jonathan Brinley | 2008-10-30 16:23
The “Useful WordPress Function of the Day” award goes to query_posts. This function can be used to:
- Revise the query that WordPress forms from the URL, so you can change the sorting of posts, exclude certain categories, etc.
query_posts($query_string . "&order=ASC&category_name=Libraries");
This takes the current query and sorts it in ascending order, limiting the results to posts in the “Libraries” category.
- Create custom queries, either for public-facing pages or in administration plugins.
query_posts(array(
"category__in" => array(1,3),
"posts_per_page" => -1,
"author" => 5
));
This query grabs all the posts by author 5 in categories 1 or 3.
After calling query_posts, you can use your standard WordPress loop, along with all the template tags it makes available, in your template or plugin.
Categories: Coding | No Comments
Jonathan Brinley | 2008-10-28 11:10
My previous post teaches you how to use jQuery and Ajax for the administration pages in your WordPress plugins. To use them in your user-facing pages requires a few changes.
We’ll use here a simlarly contrived example. Let’s say you use <!--more--> in your longer posts so they don’t fill up too much of your page. Normally, clicking the “Read more…” (or whatever text you use) link takes the user to a separate page with the complete post. In our example, rather than sending the reader to a new page, we’ll make an Ajax request to get the rest of the post and insert it directly into the current page. More…
Categories: Coding | 14 Comments
Jonathan Brinley | 2008-10-27 13:57