The previous article in this series explained the basics of writing WordPress plugins: how to create the header, and write some simple PHP code that can operate as an action or a filter (and possibly both). In this tutorial, we’ll explore actions and filters in more depth.
A filter generally allows your plugin to operate on text, before the text is either displayed or added to the database. The basic syntax for a filter is
add_filter(‘select_text’, ‘function’);
which makes the plugin code operate on the selected portion of the text using the given function. The
- the_content refers to the entire post before it is displayed on the screen
- the_tags refers to the tags retrieved from the database, before being displayed
- wp_title refers to the blog page title (not the post title) before being sent to the browser
- phone_content refers to a post submitted by email before saving to the database
There’s a similar comprehensive list for actions. Actions are actually preset occasions when your plugin can be called from the main WordPress program. This lets you trigger execution of your code, say, whenever a post is updated. Again, a short list of action events (known as “hooks”) should help give you some ideas for plugins:
- edit_post runs when a post or page is updated/edited
- publish_post runs when a post is published, or if it is edited and its status is “published”
- save_post runs whenever a post or page is created or updated
- trackback_post runs when a trackback is added to a post
- wp_head runs when the template calls the wp_head function
- loop_end runs after the last post of the WordPress loop is processed
The syntax here is essentially
add_action ( ‘hook’, ‘function’ );
which signals WordPress when to execute your plugin code.
Let’s take a look at a few code examples, remembering that to test this code in a WordPress installation, you’ll have to put it into a file in the plugin directory with a “legal” header, and activate the plugin.
Search engine optimization (SEO) involves many parts of a web page, but one of the most important is the page title. Using the wp_title text selector, we can construct a simple plugin based on a filter that will concatenate some extra text to the title of every page on our site:
<?php/*
Plugin Name: SEOptimize
Description: This plugin adds a custom string to your page title
*/
function seo($text) {
$text = $text .= ” Now SEO Optimized!”;
return $text;
}
add_filter(‘wp_title’, ‘seo’);
?>
Clearly this particular phrase will not impress Google any more than “Look here Google” would, but it illustrates that WordPress plugins are not limited to affecting the body text alone, and of course you have all the power of PHP at your disposal for text processing of any kind.
For an example of an action hook, let’s say you manage a blog and you have a number of authors, and you want to be notified whenever anyone adds a post, so you can take a look at the post and publish it as soon as possible. This plugin is a bit more complex, but still conceptually simple: it runs only when a post is saved, based on the action hook save_post in the last line. All the rest is formatting, and designed to demonstrate a number of ways of interfacing PHP code with WordPress functions.
The basic PHP mail function is used to email the notification; for that you only need a target email address, subject, and text of the email. Using the WordPress get_option function lets you retrieve the blog title directly from WordPress, rather than hard-wiring it into a string the way we’ve had to do with the email address.
Getting the title of the post is easy, since the save_post hook passes the post_ID to our notify function anyway. All we have to do is look through the documentation for post_ID and we quickly find that the get_post info function does what we’re looking for, even conveniently returning a text string.
The get_post function also enables us to retrieve the author information with no extra charge, but here there’s a minor problem: it returns the author’s integer ID from the database, not exactly human-friendly. A bit more noodling around the WordPress documentation comes up with a related function, get_userdata, which returns the author’s name as a string if you give it the author ID. So coming up with a printable name here becomes a two-step process, and now we have a nice usable plugin (the code is shown below).
<?php/*
Plugin Name: Notify
Description: This plugin notifies you by email whenever a post is saved
*/
function notify($post_ID)
{
$my_email = ‘you@yourmail.com’;
$subject = ‘New post on ‘ . get_option(‘blogname’);
$t1 = ‘New story: ‘;
$title = get_post($post_ID)->post_title;
$t2 = ” was just posted by “;
$authnum = get_post($post_ID)->post_author;
$author = get_userdata($authnum)->user_nicename;
$text = $t1 . $title . $t2 . $author;
mail($my_email, $subject, $text);
}
add_action(‘save_post’, ‘notify’);
?>