Wordpress Tutorials! 2nd Day!!
Plugin hooks are the main way to get plugins to “go” in WordPress. Spread throughout the WordPress code are hooks that can call your plugin code. There is one that is called whenever a user posts a comment. There is one that is called whenever a new user registers. Pretty much any WordPress action you can think of has a plugin hook waiting for plugins to take advantage of. You can get a list of hooks over at the WordPress Codex. This list is currently in-production, but will fill out in time. There are two types of hooks, Actions and Filters. Actions are triggered when a specific WordPress action takes place, like when a new post is submitted. WordPress doesn’t ask for any information back from the plugin, it just tells the plugin “this thing happened” and moves on. With Filters, WordPress expects that your plugin is going to process a certain chunk of data and return it, like displaying the text of a post. WordPress will pass the text of the post into the plugin, the plugin will process the text and return it, and WordPress will display the processed text. Since more than one plugin can attach to the same hook, each plugin can make the changes it needs to the filtered text. We want to a Filters because we’re going to cause some specific text in the post to pop up an image. We’re going to do this by putting some tags around the text. Using the “the_content” filter is our best bet. Here’s how to plug into a hook. Add this under your plugin header: function acropop_the_content($content) { return $content; } ?> This code adds a “sink” (think about where heat goes - into a “heatsink“) to the filter event “the_content” so that any time WordPress wants to display post content, it will pass the post content to a function named “acropop_the_content”. This is called “registering a plugin sink“. You can see the empty acropop_the_content() function, too. The function acropop_the_content() is a sink for the “the_content” hook. I mention this just so that we all know what we’re talking about. Note that this plugin will now run, although it doesn’t actually do any processing. Just upload it to your plugins directory (/wp-content/plugins) and activate it in your WordPress admin console. If you remove the return $content; what do you think will happen? All of your post text will appear to have been deleted. Don’t panic! Remember that WordPress expects filters to return their values so that they can be sent to the browser. Just put that line back and everything will be fine. Try this for fun: Replace return $content; with return Kumquat;. See what it does? I think you may be starting to get the idea. When you’re done playing with your posts, move on to the next page, where we’ll discuss finding “acronyms” to pop.