Custom WordPress Hooks and Filters in Thematic 0.6

Thematic version 0.6 is an upgrade I didn’t intend on making so quickly and there’s been some changes. Here’s a weird one: when every other WordPress theme author is desperately trying to add more and more settings to their theme options page, I took an option out. Even weirder: it made the theme better. How? I started adding custom hooks and filters to Thematic.

This is something I’ve wanted to get started on for a while so I’m glad it’s finally here.

Custom WordPress Hooks in Thematic 0.6

There are two new custom WordPress hooks in Thematic version 0.6: thematic_belowheader() and thematic_abovefooter(). You can find them in header.php and footer.php, respectively. I’m really excited about this small—but powerful—addition to Thematic. Almost anything you can think of can now be loaded into the theme, just below the header and just above the footer, by taking advantage of your Child Theme functions.php.

Now, I haven’t done anything exciting or creative with this yet—that I can show you—but here’s the outline of some functions that’ll get you started on taking advantage of the new hooks. Remember, these go in the functions.php of your Child Themes.

// Hook into the area below the header
function childtheme_helloworld() { ?>
     <h2>Hello World!</h2>
<?php }
add_action('thematic_belowheader','childtheme_helloworld');
// Hook into the area above the footer
function childtheme_goodbyeworld() { ?>
     <h2>Goodbye Cruel World!</h2>
<?php }
add_action('thematic_abovefooter','childtheme_goodbyeworld');

There you have it. That’s all you need to get started with the Thematic custom WordPress hooks. Fool around with it. Try it out with WordPress conditional tags. See what you can come up with.

Now for the filters. I think you’ll be impressed with the power filters give you over your theme.

Custom WordPress Filters in Thematic 0.6

With the introduction of the functions thematic_postheader() and thematic_postfooter() in version 0.6, Thematic has become a WordPress theme chameleon. A chameleon you control. Want your categories to show up above the title like in a magazine? You’ve got it. Want to add your own custom wording after a post? You’ve got it. Now, you can filter the output of the content above and below a post.

Since filtering requires inserting new code, I thought I’d get you started with some samples. Here is the actual code I use here on ThemeShaper to filter Thematic and simplify my post headers and footers. Copy and paste these into the functions.php of your Child Themes and start fooling around—on your test site.

First, the simplified post header that removes the author link. Check out how I’m using WordPress conditional tags to control the output.

// Add a custom post header
function childtheme_postheader() {
    global $post; 

    if (is_page()) { ?>
        <h1 class="entry-title"><?php the_title(); ?></h1>        
    <?php } elseif (is_404()) { ?>
        <h1 class="entry-title">Yikes! Not Found</h1>        
    <?php } elseif (is_single()) { ?>
		<h1 class="entry-title"><?php the_title(); ?></h1>
		<div class="entry-meta">
			<span class="author vcard"><?php $author = get_the_author(); ?><?php _e('By ', 'thematic') ?><span class="fn n"><?php _e("$author") ?></span></span>
			<span class="meta-sep">|</span>
			<span class="entry-date"><abbr class="published" title="<?php get_the_time('Y-m-dTH:i:sO'); ?>"><?php the_time('F jS, Y') ?></abbr></span>
		</div><!-- .entry-meta -->
    <?php } else { ?>
		<h2 class="entry-title"><a href="<?php the_permalink() ?>" title="<?php printf(__('Permalink to %s', 'thematic'), wp_specialchars(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></h2>
		<?php if ($post->post_type == 'post') { // Hide entry meta on searches ?>
		<div class="entry-meta">
			<span class="author vcard"><?php $author = get_the_author(); ?><?php _e('By ', 'thematic') ?><span class="fn n"><?php _e("$author") ?></span></span>
			<span class="meta-sep">|</span>
			<span class="entry-date"><abbr class="published" title="<?php get_the_time('Y-m-dTH:i:sO'); ?>"><?php the_time('F jS, Y') ?></abbr></span>
		</div><!-- .entry-meta -->
		<?php } ?>
    <?php }
}
add_filter ('thematic_postheader', 'childtheme_postheader');

Quick tip: You could use WordPress conditional tags to control when add_filter is recognized by WordPress. Like if you wanted to only filter the Post header on the index or single posts for a special graphic effect. You could even use the filter to add gravatars for the post author.

And now, the simplified post footer that removes the categories and tags (it might not look simple but it’s simpler than what shows up there by default).

// Add a custom post footer
function childtheme_postfooter() {
    global $post; 

    if (is_single()) { ?>
    	<div class="entry-utility">
    		<?php printf(__('Bookmark the <a href="%1$s" title="Permalink to %2$s" rel="bookmark">permalink</a>.', 'thematic'),
    			get_permalink(),
    			wp_specialchars(get_the_title(), 'double') ) ?>
    
    <?php if (('open' == $post-> comment_status) && ('open' == $post->ping_status)) : // Comments and trackbacks open ?>
    		<?php printf(__('<a class="comment-link" href="#respond" title="Post a comment">Post a comment</a> or leave a trackback: <a class="trackback-link" href="%s" title="Trackback URL for your post" rel="trackback">Trackback URL</a>.', 'thematic'), get_trackback_url()) ?>
    <?php elseif (!('open' == $post-> comment_status) && ('open' == $post->ping_status)) : // Only trackbacks open ?>
    		<?php printf(__('Comments are closed, but you can leave a trackback: <a class="trackback-link" href="%s" title="Trackback URL for your post" rel="trackback">Trackback URL</a>.', 'thematic'), get_trackback_url()) ?>
    <?php elseif (('open' == $post-> comment_status) && !('open' == $post->ping_status)) : // Only comments open ?>
    		<?php printf(__('Trackbacks are closed, but you can <a class="comment-link" href="#respond" title="Post a comment">post a comment</a>.', 'thematic')) ?>
    <?php elseif (!('open' == $post-> comment_status) && !('open' == $post->ping_status)) : // Comments and trackbacks closed ?>
    		<?php _e('Both comments and trackbacks are currently closed.') ?>
    <?php endif; ?>
    <?php edit_post_link(__('Edit', 'thematic'), "nttttt<span class="edit-link">", "</span>"); ?>
    
    	</div><!-- .entry-utility -->
    <?php } else { ?>
        <?php if ( $post->post_type == 'post' ) { // Hide entry utility on searches ?>
    		<div class="entry-utility">
    			<span class="comments-link"><?php comments_popup_link(__('Leave a comment', 'thematic'), __('1 Comment', 'thematic'), __('% Comments', 'thematic')) ?></span>
                <?php edit_post_link(__('Edit', 'thematic'), "ttttt<span class="meta-sep">|</span>n<span class="edit-link">", "</span>ttttt"); ?>
    		</div><!-- .entry-utility -->
        <?php } ?>				
    <?php }
}
add_filter ('thematic_postfooter', 'childtheme_postfooter');

I don’t think any WordPress theme is currently filtering the content above and below their post content. They should be. It’s powerful stuff and puts an amazing amount of control into the hands of WordPress users willing to experiment with Child Themes. Have fun. Use it. I am.

Start Taking Control of Your Theme

I hope you can see the benefits of filtering your WordPress theme and adding taking advantage of custom WordPress hooks. And I hope the code above is enough to get you started working on yours. Check out the latest version of Thematic. Experiment and have fun. Take some chances. That’s what WordPress theming is all about.

42 thoughts on “Custom WordPress Hooks and Filters in Thematic 0.6”

  1. I’ve been playing around with hooks a lot lately. I haven’t really gotten into filters just yet though. Hooks and filters really open this stuff up and give you an endless amount of potential.

    I really wish I would’ve thought about them before I added a theme option to set your navigation below or above the header. It would’ve given me a lot more leeway with child themes.

    Now, the trick is to let your users hook into or filter this stuff from a theme options page. 😉

  2. @Justin Tadlock: If you’re thinking about getting into filters, download Thematic and check out hooks-filters.php. After you finish crying, try them out. Like you said, it really gives you a lot of leeway with child themes.

  3. Ian – I’ve been thinking about switching from Sandbox to Thematic because of all the seductive options for customizing that you keep coming up with…this post might just tip me over the edge into commitment. How difficult will it be for me to bring my current customized Sandbox skin to Thematic? Does your use of Blueprint/Triploli mean I have to painstakingly redo my style sheet? Thank you for sharing these ideas, and I wish you the best with Travailler.

  4. @Erin: It shouldn’t take you too long at all. You don’t have to actually use any of the included stylesheets. You can just use the ones you’ve developed for your existing themes. Nothing is forced on with Thematic. Except the markup. Most of the markup is from The Sandbox (why mess with perfection?) so you shouldn’t have trouble with it.

    Two things to watch out for: the post meta and utility areas and the author page. And if you’re using all the widget areas you’ll want to look at the default style.css for some tips.

  5. @Justin Tadlock: I should also point out that just surfing around, looking at different sites, I find myself thinking, “Hey, I bet I could do that with Thematic 0.6!” Hooks and Filters really make a theme powerful.

    Looking forward to seeing what you come up with.

  6. I’m using Subversion for Thematic to keep up with updates. Why download when there’s a much easier way? 😉

    I’m working on a plain ol’ blog theme, and I think I’ll try to put together my current blog as a child theme as you have here at Theme Shaper. It would be a neat experiment with hooks and filters.

    One of the things I’m using hooks for right now is to take all the crud out of the theme files. Even though I try to keep users out of those files, it’s much easier for them to modify things if they don’t have to look at all that PHP, which just confuses a lot of them. So, another great thing about hooks and filters is organization and cleanliness.

  7. Nice to see more themes doing this. Hopefully plugin authors will figure this out and stop asking people to hack their templates into little bits (with all the attendant problems for upgrading the theme) just to add a function call.

  8. Fantastic! I think this is a very useful practice. If every template designer could follow your advise, that will be a dream world for the plugins programmer. If the template can support enough actions and filters, the normal user of the wordpress will never need to deal with code when they install a plugin.

  9. Hi Ian,
    So with this is it possible to add a sub header that displays a custom header for each page. For example the about page has a diffrent subheader image than the contact page and so on.

  10. hello, i have a question, i’m a programer, i reside in uruguay south america, so sorry my english and i hope you can understand me, i try put parameter rel=’nofollow’ in all links on every post, wordpress already do this in comments, but not in the post, i want to do this for any post of any user, if you know some thing about this, i apreciate your help, thanks!!!

  11. thanks, yes i found a plugin, but does’nt work as i want, if anyone knows any please give me the link!! thousand thanks!!!

  12. I am by no means a programmer but love the way both of the filters cleaned up the look of my posts. How would I go about hiding “by author” and the meta separation? I don’t have a multi-person blog so it’s pretty obvious that I wrote it. I looked around but didn’t notice anything obvious that would point me in the right direction.

    thanks!

  13. I hope that asking another question doesn’t make me greedy. If I wanted the number of comments to display as a written out word instead of a number (two instead of 2), how would I go about doing that?

  14. Hi Ian, I found a solution in the net, didn’t know, that it is so easy:
    <!– –>
    Thanks!

  15. this is all well and good, but for someone who over the years figured out how to make simple changes to php out of necessity in order to make changes to various templates, this is way over my head…all I want to do is remove the category information on each post and this is way too complicated…

  16. I’m new to PHP. I like the “Add a custom post footer” here. How might I add the ability to show Tags back into this code?

    1. I have the same question as Brice: how would tags be added back in to that great footer code?

    2. Spending a few minutes looking at the current Thematic Post Footer function is probably the best explanation. If you run into any trouble, just post a new topic about it in the forums.

      1. Ahh, my discomfort with PHP must be showing 🙂

        Point well-taken. I’ll try and get my head around it. Thanks.

  17. Hey, this is great stuff. I’m getting all excited about Frameworks, Parent Themes, child themes and Thematic.
    I just wonder- is it possible to have 2nd level child themes? IE – let’s say I create a child theme that modifies the parent theme a lot. Now I’d like to use that one as a parent theme for my other themes. Can I do that?

  18. Ey up. Quick question – if anyone can help… I’ve implemented the thematic_abovefooter() in my functions.php. Site is here – http://clients.digitalentrepreneurs.org.uk/harrelle/ – but I’m really struggling to tailor it to go in the centre.

    Simple stuff, I’m imagining – but after trying to stick align=”center” in the .php (which is probably bad form/not gonna work) I’ve been instead trying to classify the function in the .php – – then stick in a new class in the .css – but with no success either way.

    Could anyone suggest owt or point me in the right direction? Feel like I’m running aground on the ‘sticking different types of code in the wrong files’ rocks…

Comments are closed.