close

Best Auto Insurance Rates

 <?php get_footer(); ?>

9

Theme Hooks – Actions & Filters

Hooks allow you to customize your theme in lots of cool ways by modifying or adding to the processes run by WordPress to generate your site content.

Have We Seen These Before?

If you were paying attention then you may have noticed we sneaked in a couple of WordPress hooks back in Chapter 5.

Although we didn’t go into much detail at the time we presented two incredibly useful action hooks. The first one ‘wp_enqueue_scripts’ allows you to safely add scripts to your header, and ‘wp_head’ adds code needed to be placed just inside the closing HTML tag.

Modifying your site header in this way means you don’t need to manually edit the themes header.php file at all!

 

1519
Number of WordPress hooks in Adam Brown’s .

So what’s a theme hook you ask?

In simple terms theme hooks allow you to modify WordPress behaviour (and output) without having to hack ANY of the core files.

This may not sound like much of a big deal but you can do some pretty powerful things using hooks, and they are extremely flexible too!

Hooks come in two basic flavours: , and . Whilst they both allow you to alter the default behaviour of WordPress in some way they are quite different from one another. Oh, and there are lots of them too. And I mean LOTS!

In WordPress 3.3 there are a total of over 1,500 action and filter hooks for you to modify almost every aspect of WordPress imaginable. You can see a full list of these hooks on the legendary (well in WordPress circles anyway) . (See, I told you there was a truck full!)

Deep in the innards of WordPress all these hooks are defined at very specific points in the normal execution of the core code. They are deliberately placed at key points so you can literally ‘hook’ into the core and alter the default behaviour without actually editing any WordPress files directly. Neat huh? These WordPress guys really know what they are doing!

Action Hooks

These are the hard-hitters of the WordPress hook world. Every time a certain action happens in WordPress you have the option to step in and alter the default behaviour for that particular event via an action hook.

For instance, every time a post is saved in the WordPress admin the ‘save_post’ action hook is triggered. At this particular point in the WordPress script execution you have the option to do something custom. This is why hooks are so powerful; and good luck to you trying to do this without using a hook!

Firstly you would have to find the right place in the maze of WordPress code, and secondly your changes are in a fragile state of existence because as soon as an organized administrator hits the update WordPress button your changes could be overwritten in an instant!

All other action hooks work in a similar way. You get the idea, right?

Filter Hooks

Filter hooks are more subtle creatures than actions hooks. You typically take some WordPress content (e.g. the post content) and modify it somehow, before returning back into the WordPress mix.

For example, if you wanted to modify the post excerpt then just use the ‘the_excerpt’ and you can modify it as you need and customize its appearance.

The important thing to note here is that you MUST return whatever value is passed to a filter hook (plus your modifications of course) or you could break some functionality of your site. Don’t panic though; you will be fine as long as you always return an expected value.

Show Me Some Code Already!

OK, so enough theory. Let’s see some hooks in action shall we (pardon the pun!)?

By now you should have a child theme with a functions.php file defined. If not, you can download a sample .

Let’s say that you wanted to add some custom content to the bottom of each page on your site. For this you could use the ‘wp_footer’ hook which adds code directly before the closing HTML tag.

Right, so we know what hook we want to use but how do we write this in code? Take a look at the following:

1
add_action( 'wp_footer', 'pc_custom_footer' );

This single line tells WordPress that you want to do something when the ‘wp_footer’ action is triggered. The second parameter ‘pc_custom_footer’ is the hook callback function. This is just the function name that is called when the hook is triggered.

All we need to do now is to specify the ‘pc_custom_footer’ function as follows:

123
function pc_custom_footer() {

auto insurance quotes comparison new york     auto insurance quotes comparison australia


TAGS


CATEGORIES

.