Codeable info

Understanding the WordPress Plugin API

Posted on by in WordPress Tutorials

This is a tutorial for plugin developers that explains the basics of the Plugin API or Application Programming Interface. Chances are, if you are seeking to learn about becoming a WordPress plugin developer, you probably have worked with some APIs in the past. The WordPress plugin API is one that was developed so WordPress developers no longer have to use outdated hacks to add special features to WordPress. Before WordPress version 1.2, the only way to do such modification was with hacks. The term hack is no longer necessary with the WordPress plugin API because it adds hooks for developers to link directly into the WordPress core.


Hooks are the WordPress solution for “hooking into” the rest of WordPress with a plugin. Plugins can be created by anyone who knows how. Becoming a plugin developer, opens up a few avenues for developers to make a profit. Those avenues include developing plugins for any clients you may have and also developing plugins for the open market. There are two kinds of hooks in the WordPress Plugin API. They are actions and filters.


Actions are hooks in which the WordPress core launches when specific events occur. There is an Action API that developers can use to define what PHP function are executed during certain predetermined actions of the WordPress core. Examples of actions include the publishing of a post, showing of a page and even changing themes from the admin. Actions exist for the admin as well as the front end of a blog. There are three basic steps to adding an action to a WordPress site. They are as follows:

  1. First, add a PHP function that is to occur when a specific action is called.
  2. Second, add the PHP function you wrote in step one to the WordPress core by using the add_action() call to hook into the rest of WordPress.
  3. Insert the function into a PHP plugin file and activate the plugin.
  4. So to actually add an action that does something when a specific event occurs, you start by writing your function like this:

    function do_something(post_ID) {
    //add whatever you want to do when the function is called
    return $post_ID;

    Then simply add the function by calling add_action() like this:


Once those two are in your plugin, you have a working action function added. Actions are called with the do_action function once they have been added as we did above. So to call our action into play, we would use something like this: do_action($do_something);


Filters are hooks that WordPress uses to modify text. Specified blocks of text can be modified by a filter either before the text is saved to the database or before it is sent to the user’s monitor for viewing. Filters basically filter text coming from and going to the database to alter it in a desired fashion. Here are the two basic steps to creating a filter for any WordPress site:

  1. Create a PHP function to filter the text. An example function could look something like this:

    function filter_post_text($content) {
    $badwords = array(‘arse’,’hell’,’…’);
    $content=str_ireplace($badwords,'{censored for content}’,$content);
    return $content;

    That function can be used to replace any list of words with something else. It would be a good function for editing foul language out of your WordPress posts. So every time it sees a words in the “badwords” array, it replaces it with ‘censored for content’. You can change it to say whatever you want if you use this function yourself.
  2. Hook the filter into the WordPress core using the add_filter() function like this:


    You will have a working filter once you have added the above lines of code to your plugin file.

After the filter has been added, you can call it with the do_filter function.

Action and Filter functions

There are a lot of existing action and filter functions in the WordPress core you can use for plugin development. To see the list for each type, visit the WordPress codex at: for actions

and for filters


After reading this tutorial, I hope that you will have a better understanding of the WordPress plugins API and even be able to add some simple functions or create your own plugin with what you have learned. I wish you all the best of luck in learning to write WordPress plugins. Don’t forget, the WordPress codex is a great place to start whenever you need help. Have fun!

Comments Closed

Codeable info