Getting Started with the WordPress Plugin API

Posted May 2, 2013

There may come a time in your WordPress career when you decide it's time to write your first plugin. Whether you can't find a plugin that does exactly what you need or you just feel the urge to get into coding, don't be afraid of giving it a shot. You probably won't be writing a forum or caching plugin right away, but the sooner you get started the sooner you'll be making progress.

WordPress provides hooks for plugins to extend and modify its default behaviors. The two types of hooks are actions and filters:

  1. Actions are functions that get triggered during certain events like 'save_post'
  2. Filters are used to modify information being saved to the database or displayed to the user

Today we'll be learning how to write our first plugin by using a filter to modify the title displayed for breaking news posts. News sites sometimes add "BREAKING" to the title of certain posts, and instead of doing this manually we'll learn how to automate the process. When we're done, you'll be able to tag a post with "breaking" and for the first 24 hours the title will automatically be prefixed with "BREAKING."

WordPress uses PHP, so you may want to learn a bit about its syntax, but it doesn't take long to get the basics and can be picked up from examples.

The first step to building a new plugin is to create a new file in wp-content/plugins/. We'll call it breaking-news-titles.php. You'll need an FTP client, but if you installed WordPress yourself you're probably familiar with FTP.

Next open up the file in a text editor. Since we'll be writing in PHP, all of our code should be wrapped inside PHP tags.

<?php

// Our code will go here.
// (lines beginning 2 slashes are comments in PHP)

?>

The first thing we'll be putting into our plugin file is some information telling WordPress about the plugin.

/*
Plugin Name: Breaking News Titles
Plugin URI: http://refactored.co/blog/getting-started-with-the-wordpress-plugin-api
Description: Dynamically add BREAKING to the title of select new posts
Author: Refactored Co.
Author URI: http://refactored.co
Version: 0.1
License: GPL2
*/

Now that WordPress knows about our plugin, we'll need to add a function that can modify our titles if it needs to. This function takes two arguments, $title and $post_id, and returns the title we want to display.

// Breaking news title filter
function refactored_breaking_news_title( $title, $post_id ) {
    // Test if the post is tagged 'breaking'
    // and if it was published in the last 24 hours
    if ( has_tag( 'breaking', $post_id ) && time() - get_the_time( 'U', $post_id ) < 60*60*24 ) {
        // Modify the title
        $title = 'BREAKING: ' . $title;
    }
    // Return the title
    return $title;
}

You'll notice that there's an "if" statement in our function. We're using this to make sure we only modify the titles of the posts we want. This not only tests if the post has the tag "breaking," but also tests to make sure the post is less than 24 hours old. To check how old the post is, we're subtracting the time the post was published from the current time. To make things easy, we're using Unix time which is measured in seconds. We then check to make sure it's less than the number of seconds in a day (606024). If both of these conditions are true, we'll prefix the title with "BREAKING:".

Now that we have our filter function written, we need to tell WordPress about it.

// Add our filter to 'the_title'
add_filter( 'the_title', 'refactored_breaking_news_title', 10, 2 );

At this point you should have a working plugin! Here is it all together:

<?php

/*
Plugin Name: Breaking News Titles
Plugin URI: http://refactored.co/blog/getting-started-with-the-wordpress-plugin-api
Description: Dynamically add BREAKING to the title of select new posts
Author: Refactored Co.
Author URI: http://refactored.co
Version: 0.1
License: GPL2
*/

// Breaking news title filter
function refactored_breaking_news_title( $title, $post_id ) {
    // Test if the post is tagged 'breaking'
    // and if it was published in the last 24 hours
    if ( has_tag( 'breaking', $post_id ) && time() - get_the_time( 'U', $post_id ) < 60*60*24 ) {
        // Modify the title
        $title = 'BREAKING: ' . $title;
    }
    // Return the title
    return $title;
}

// Add our filter to 'the_title'
add_filter( 'the_title', 'refactored_breaking_news_title', 10, 2 );

?>

Hopefully you've been able to follow along and learn a little about the WordPress plugin API and PHP. If you don't have an idea for your own plugin yet, you could try expanding our breaking news plugin a bit. You could have the "breaking" tags remove themselves after the 24 hours or create some type of tags that would apply to your site.

If you're having any trouble or want to start getting more advanced, check out Writing a Plugin on the WordPress Codex where you can find many other helpful resources for your journey.