Monthly Archives: October 2008

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.

jQuery and Ajax in WordPress Plugins – Public Pages

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. Continue reading

jQuery and Ajax in WordPress Plugins – Administration Pages

This is a quick overview of how to use jQuery and its Ajax functions in WordPress. To get the point across, I’ll use a simple and contrived example. We’ll have an admin screen with a list of categories. Clicking on the name of one of the categories will fetch a list of titles of posts in that category and display them as a sub-list of that category. Continue reading

700,000,000,000 Is a Very Big Number

I’ve no desire to get into the consequences/benefits of the recent financial industry bailout/rescue on this blog. Yes, I have opinions about it, but the opinions of a librarian and web developer with formal training in neither economics nor politics matter little in this discussion.

What I do have to say, though, is that $700,000,000,000 is a lot of money. So much so that I don’t know how much money it is. I think of money in terms of things/services I can exchange it for, as it really has no other use (unless you burn it to heat your home). I know what $1 will get me: a few bananas, or perhaps a used book. With $100, I could buy a couple of weeks of groceries or pay for a visit to the doctor.

Beyond that, money starts getting a little more abstract. I don’t need 4 years worth of groceries at once, so I can’t imagine spending $10,000 to buy those groceries. At that level, I can’t think of money in terms of groceries any more; I have to move on to larger, more expensive items, like a car, or a marimba, or a house.

And that’s where my concept of money starts to break down. A million dollars is about the most that I can conceive of; anything more than that is too abstract. The sun is 93,000,000 miles away. How many times will I have to drive to work before I drive 93,000,000 miles? It will take me about 25,000 years, it looks like. A billion is a meaningless number; you might as well say “a lot”. 700 billion is just “a lot more”. What’s the difference?

I certainly don’t know, and I doubt anyone dealing with our economic woes does, either. We’re just throwing big numbers around hoping that they’re big enough that people say, “Oh, that’s a really big number! It must be important!” If everyone believes the number is big enough, faith in the credit markets will be restored, and home prices will increase 40% a year forevermore.