Using Hooks In WordPress

Posted on · Tagged in

Every so often I have clients who want me to work on WordPress sites or plugins. I honestly hate working with WordPress. While it’s one of the most popular CMS products in the world with a massive developer base, I find that anything coded for WordPress tends to follow a tacky and poorly organized form of coding. Coding styles vary wildly from developer to developer and there really is no standard. This makes doing modifications a nightmare.

As crazy as WordPress development is, it makes up for it with some very helpful features for developers. In the early days of WordPress, if you wanted to add or modify functionality you had to modify the core of WordPress. This is dangerous because as soon as an update comes out, your modification might get wiped out. WordPress then introduced theming and plugin functionality. Along with those features, they allowed for developers to ‘hook’ into actual WordPress processes without modifying the core source code.

The main concept that WordPress developers should be familiar to is ‘The Loop’. Basically every page you go to on a WordPress site is the result of ‘The Loop’. It’s a set of steps that WordPress goes through to build page you are viewing. For example to view a single post, WordPress might pull the post from the database then call a header() function which pulls in the header template then a content() function and a footer() function then finally rendering the page.

Let’s say that we want to add some extra meta tags to every wordpress page? How would we go about this? First we decide which process we want to hook into. WordPress has quite a few hooks. For this example we’ll use the ‘wp_head’ hook. To use a hook we use the ‘add_action’ function.

add_action('wp_head', 'insert_meta_tags');

function insert_meta_tags() {
    echo '<meta property="info" content="Some info" />';
    echo '<meta property="info2" content="Some more info" />';

Basically you specify the hook you want to access and then a function to be called. Once WordPress hits the ‘wp_head’ portion of ‘The Loop’ it will call the function that you registered. This particular hook is called an action hook. There is another popular hook type called a filter hook. Filter hooks are somewhat different in that they are used to modify specific content like your post title or content. To use a filter hook, we call the ‘add_filter’ function.

add_filter('the_content', 'add_some_stuff_to_content');

function add_some_stuff_to_content($content) {
    $content .= 'Blah blah blah';

    return $content;

When you create a filter your callback function is passed the variable that you’re modifying which is in this case, the content of a post. In this case we appended ‘Blah blah blah’ to it and then returned it.

This is all really just basic stuff but hopefully it gives you an idea of how you can make useful modifications to WordPress.


comments powered by Disqus
Subscribe to my newsletter and get a free copy of my book, Aspect Oriented Programming in PHP.