Tag Archive: plugins

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.

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