Using actions in your WordPress themes

At some point, every major theme developer wants to do “specific” things in his theme(s). Either you want to add specific images, need several meta’s per post or you want to move some of the “utils” like edit this post around.

I’ll be blogging a bit about the several things I do in my theme development work (I do occasional freelance work for WooThemes, for instance), and the first thing I want to show you is how I use WordPress built-in possibility to add an action to your theme, to improve my own user experience.

By default WordPress shows an “edit this post” link after each post or page, which looks a bit dull and is not really in the position I’d want it to be in, so I’ve decided to move it around a bit, and move some of my other icons along with it.

The first thing I did was add an action above my title heading on every page template like page.php and on single.php, like this:

<?php do_action("pre_the_content"); ?>
<h1><?php the_title(); ?></h1>

Then I opened up my themes’ functions.php and added the following:

function yoast_edit_link() {
  if (is_page() || is_single()) {
    edit_post_link('<img src="'.get_bloginfo('template_directory').'/images/edit.png" 
      alt="edit this page"/>', '<p style="float: right; margin: 0 2px;">', 
      '</p>');
  }
}
add_action("pre_the_content","yoast_edit_link");

Now, everywhere I add the pre_the_content action hook, this will display the edit post / edit page link with a cool icon, in my case, a 22×22 icon found on iconfinder.net, made by Everaldo Coelho.

Now this is all in place, I can start adding other stuff. My email commenters plugin had an ugly line below the post as well, so I decided to replace that in my version with another icon in the same area, specifically this icon, made by the same Everaldo.

Another thing I decided to move up there are the bot stats created by , for which I used this icon, also made by Everaldo (he’s really good).

The end result looks like this on my posts and pages:

utility-icons-theme.png

So you could use the same way of adding actions to your themes, to make it easier for you to add in functionality at specific points in your themes.

Tags:


Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

5 Responses

  1. JauhariBy Jauhari on 13 August, 2008

    Really Useful I will try this tips ;)

  2. PaulBy Paul on 13 August, 2008

    I just discovered your site. Fantastic.

    You seem to have gone one stage further than everyone else in changing the calendar icon each month on your blog.
    Any chance of a a few tips for doing that?

    Thanks.

Trackbacks