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).

<?php
/*
Plugin Name: My Blog Tweaks
Plugin URI: 
Description: Different bits of custom things I like to do on my blog
Version: 0.1
Author: 
Author URI: 
*/

// Add a Share on Twitter link
function myblog_shareontwitter($content) {

    print $content; ?>
    
    <p><a href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Click to send this page to Twitter!" target="_blank">Share <em><?php the_title() ?></em> on Twitter</a></p>
    
<?php }
add_filter('the_content', 'myblog_shareontwitter');

?>

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.

/*
Plugin Name: My Blog Tweaks
Plugin URI: 
Description: Different bits of custom things I like to do on My Blog
Version: 0.1
Author: 
Author URI: 
*/

Now for the real meat of the plugin: a custom function that filters the content of your WordPress blog.

// Add a Share on Twitter link
function myblog_shareontwitter($content) {

    print $content; ?>
    
    <p><a href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Click to send this page to Twitter!" target="_blank">Share <em><?php the_title() ?></em> on Twitter</a></p>
    
<?php }
add_filter('the_content', 'myblog_shareontwitter');

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:

    print $content; if (is_single()) { ?>
    
    <p><a href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Click to send this page to Twitter!" target="_blank">Share <em><?php the_title() ?></em> on Twitter</a></p>
    
<?php } }

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.

For a list of all the things you can filter in your WordPress theme check out the full list of WordPress filters.

24 thoughts on “Customize Your Posts Without Touching Your Theme Files”

  1. Ian,
    Not to nit-pick, but because WordPress usually uses the filter to assign a value to a variable:
    $content = apply_filters(‘the_content’, $content);

    …the function you hook into the filter would be faster if you only returned a new value. For instance:

    $content .= ‘stuff you want to append to the end of the $content variable’;
    return $content;

    Nathan

  2. Reading this post actually gives me an idea. Frivolous, maybe, but an idea nonetheless.

    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.

      1. This is an interesting interaction between you two. Thanks for showing this information Ian.. The power to edit themes w/out editing the theme files is definitely a great way to go.

    1. 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.

    2. I’ve actually done this too. I have a bucket load of custom plugins to do little jobs, and ironically, I’m looking into moving them into my theme!

      1. Stephen, I have a different bucket load, but it’s still a bucket load. It’s a great technique overall, but I’m at about my limit for the number of personal plugins I want to keep up with.

  3. I tried this out using a “virgin” install of Thematic and there appears to be a conflict. The new content regurgitates into the header of my page, right here:

    meta name=”description” content=”RIGHT HERE”

    Is that because the single page meta description is calling this?

    the_excerpt_rss();

      1. I just stripped out all the other plugins just to confirm. This is what a single post page looks like:

        It doesn’t happen to the main index. I am just going to strip out the the_excerpt_rss(); in the header.php for now.

  4. Hi..

    I wonder is there any chance to remove unnecessary post infromation (like author, gategories etc.) with these little plugins?

    Or what should I do? I want my posts showing only title, date and post content.. I also want to remove “Posted in Uncategorized” etc. under the post.

    I’m using wp as a cms so I don’t want show anything unnecessary in my posts..

  5. 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).

    http://core.trac.wordpress.org/browser/trunk/wp-includes/comment.php#L1419

    Also, here’s a list of filters that would potentially be broken by your method:

    http://core.trac.wordpress.org/browser/trunk/wp-includes/default-filters.php#L105

    I think it would be better (and more along the expected WordPress method) to append your string and return everything to the calling code.

  6. 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?

  7. 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!

Comments are closed.