Tag Archive: WordPress

Custom Permalinks for Custom Post Types in WordPress 3.0+

Update 2010-10-04: See Understanding Rewrite Tags for a somewhat more helpful treatment of the subject.

I’ve been playing around with WordPress 3.0 to create a bug/issue-tracking plugin (i.e., using WP to track bugs, not to track WP bugs (although it could conceivably do that)). More details about the plugin will be forthcoming, but now is as good a time as any to share the solution to one problem I came across.

Creating New Post Types and Taxonomies

When you create a new post type, you use the register_post_type function. When you create a new post type through that function, WP creates a new rewrite rule base on the name you give the post type. In my case, I’ve created an issue post type, so all issues, by default, will have the permalink /issue/%issue%/ (where %issue% is replaced with the post slug). Using the rewrite argument to register_post_type, you can change the beginning of the permalink to something different (e.g., /bugs/%issue%/) should you so desire.
Continue reading

Save Time with sed

When I develop WordPress sites, I find that I end up repeating many identical tasks for each site. The phrase “repeating identical tasks” should (and does) set off alarms: This should be automated! To that end, I’m trying to learn some more command line tools for doing tasks that I currently use a GUI for.

One such task is migrating my development database to the staging/production server. Basically a mysqldump from my local database that I can then import on another server. A key thing to watch out for with WordPress, though, is the base URL for your site. If you’re serving a site from localhost while you develop it, you’ll need to change every occurrence of localhost in your database to the base URL for the new server.

sed is a command line tool for doing just that job. It runs a regular expression search on the input and outputs the replacement. Example:

$ echo "localhost" | sed "s/localhost/www.example.com/g"
www.example.org

Pipe your mysqldump through sed to have an SQL file ready for your new server.

$ mysqldump -uusername -ppassword database_name | sed "s/localhost/www.example.com/g" > database.sql

This way I avoid having to open the SQL file in a text editor, doing a global search and replace, and re-saving.

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

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.