Custom Thumbnails WordPress Plugin

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. Continue reading “Custom Thumbnails WordPress Plugin”

Static Front Page for Your WordPress Site

WordPress makes it incredibly easy to set a static page as the front page for your blog or website; so easy, in fact, that it’s almost not worth mentioning here. But there’s one tricky and poorly documented bit, as you’ll see shortly.

If you go to Settings » Reading in you administration panel, you’ll see at the top of the page a section labeled “Front page displays”. It gives you an option to show your latest posts or a static page, with the former chosen by default.

Screenshot from before setting a static front page

If you want a static front page, you can simply publish a page (we’ll call it “Home”) and then choose the latter option with the “Front page” option set to “Home”.

Now, what URL do you use to get to your list of posts? What if you want to have a static home page at http://example.com/ with your blog at http://example.com/blog/? It took me a while to find the answer, including some unsuccessful attempts at modifying $wp_rewrite->rules. It turns out the answer is incredibly simple: publish a page called “Blog” (it doesn’t have to have any content) and set the last option, “Posts page”, to “Blog”. If you want all of your posts’ URLs to appear under http://example.com/blog/, too, just prefix your permalink structure with /blog.

Screenshot from after setting a static front page

DOAJ Export WordPress Plugin

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.

Custom Feeds in WordPress

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.

Make a New WordPress Loop with query_posts

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.