Customize Your Posts Without Touching Your Theme Files

I hate editing my WordPress theme files—and I’m a theme developer! It makes it a pain to update the theme when a new, better version comes out. And if you ever change your theme you’re either going to forget about some crucial edit you’ve made, or spend wasted hours hunting down all your customizations. Didn’t we decide to use WordPress because it was so simple to use?

Well, it is simple to use. The secret is to write a quick plugin. The really big secret is it’s not that hard.

Here’s a quick bit of code to that’ll start a quick, personal plugin for you, My Blog Tweaks. Save it as my-blog-tweaks.php in a folder called my-blog-tweaks in your plugins directory. For a start we’re going to add a Share This Post on Twitter link to each and every post on your blog using this plugin. First, all the code you’ll need (I’ll break it down a bit after).

The first part of any plugin has a commented section just like a WordPress theme. You’re intimately familar with how it outputs once you’ve seen the list of plugins in your plugin panel in the admin area of your blog.

There’s two important bits you need to know. First, in the function. what we’re doing is printing out the content—the regular content you’d see in any post—and then some HTML mixed with PHP code. The sort of thing you’d normally just stick right into your single.php file—and forget about when you changed themes.

Next, we filter the content. We use add_filter to tell WordPress we want to hijack the regular old content and put something else in there. Pretty cool, huh?

Want to show the link only on single post pages? Throw in a conditional tag:

I’ve purposely kept this sample code simple so you can adapt it to other uses. And like most plugin code, you could add the function and add_filter to your Child Theme functions.php if you’re doing something really theme specific.

That’s funny. I asked myself this same question recently after having to edit my theme’s functions.php every time a new version came out. I, however went the lazy route and used Extended Options instead😛

Using this method, you really do eliminate the need for theme modifications. In fact, you could include a stylesheet with your plugin and insert the call to the stylesheet using the wp_head action. There’s your custom user functions and user styles right there.

That’s pretty much what I’ve been doing on the Tarski site for… well, years now. I discussed it a bit in this forum thread.

It’s a nice way to hack stuff into a site with relative ease, but for anything substantial these days I’d use a child theme. The basic issue with it as a technique is that you have to do a bit more heavy lifting (dealing with filesystem vs. URI paths can be a pain), and it’s not as flexible in terms of template replacement as a child theme.

Plugins also provide a good way of bundling small changes while ensuring that users can upgrade small theme files, i.e. without requiring that they write their own child themes in order to customise their site. Here’s a collection of them that I’ve run up for Tarski. Most of them were in direct response to user questions; it’s quicker to write a plugin than to explain something, especially for the less technical people out there.

I have to agree with Nathan Rice that this isn’t exactly the “WordPress Way”. What if another filter is being applied to the_content that runs after your filter? Also, when the the_content filter is applied, the content isn’t necessarily meant to be printed immediately. Here’s an example where it’s assigned to a variable (for excerpts).

Great post, very helpful. Helped me insert my “signature” image after every post, instead of adding it to the end of content (and having to deal with my other plugins taking precedence). Thanks very much!

Thanks a lot man, I’ve been hacking wordpress themes to death recently trying to create different layouts for different categories, I think writing my own plugins is going to be the way forward. Does WordPress 3.0 help solve any of your problems?

Don’t know squat about building plugins, and I would have never thought they were created with PHP. But this post inspires me to learn more about creating them. I thought I’d have to learn a new coding language or something. Thank you!