Soo as you know i’ve recently dumped / removed some bad code off my machine and i’m replacing it with well, less bloated code.    One of the first things i’ve redone is the “Activity Monitor” code.    Basically something that reports what users are doing on the server.

To create the activity monitor what is needed to be known is that it primarily uses the do_action() and add_action() functions.   These functions in wordpress allow you to specify a point in the code to trigger or initiate another segment of code.    Like an extension to the original code that’s placed there.   This is used so that different plugins that are created can use those ‘hooks’ in the wordpress code to add to the original core code without having to actually edit the core code itself.

do_action()
http://codex.wordpress.org/Function_Reference/do_action

The Wordpress.ORG Codex refers to the usage as:

do_action($tag, $arg = );

The $tag refers to the ‘Label’ you specify took hook into with your add_action function.   The $arg = refers to the arguments that you want to pass through the do_action and into the add_action.

.. That really doesn’t say much on how to actually use the do_action.    What the do_action() function does is set’s a label in a specific point in your code for your plugin to hook into with the add_action() function.     For example with my activity monitor I want it so that every time someone buys a site in my Site-Trader game on WebComic Planet that it records that they have made that purchase into the database.

I placed this bit of code into the position right after the purchase was made:

<?php do_action('site_trader_buy_site',$tradeinfo->name,$tradeinfo_purchaseprice);  ?>

What this says is that at the point where the purchase was made and added to the database (directly after) place a label called “site_trader_buy_site” and pass 2 parameters,  $tradeinfo->name and $tradeinfo_purchaseprice.

This is the “hook”  that is now placed into the code that add_action can go and attach to.   Get it yet?   Basically your marking a location in the code so that more code can hook into it.

add_action()
http://codex.wordpress.org/Function_Reference/add_action

The Wordpress.ORG Codex refers to the usage as:

add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1);

The $tag refers to the label you specified as the hook in the do_action() function.   $function_to_add refers to what the name of the function is inside your plugin to execute, $priority refers to how much of a priority is given to execution of this function and $accepted_args refers to the number of arguments passed through do_action() to the $function_to_add.

What this is really saying?   The add_action finds the label specified in the $tag and hooks into it saying to that bit of code to “execute this function in addition to what you’re already doing, oh and pass these arguments.”  For example, in my activity monitor plugin:

add_action('site_trader_buy_site',  'sp_activity_site_trader_buy_site', 10, 2);

See how i’m hooking into the site_trader_buy_site label I specified in the do_action()?   Then i’m telling it to execute a function I have in my plugin called “sp_activity_site_trader_buy_site” with the priority of 10 (standard) and i’m passing 2 arguments from the original do_action() into the function i’ve requested it to execute.

Then, also inside my plugin, notice that i’m using the funciton name I specified and i’m passing 2 arguments from the do_action() into it.

function sp_activity_site_trader_buy_site($sitename='',$sitecost=0) {
       global $current_user;
       $sitename = addslashes($sitename);
       $sp_string = $current_user->display_name." purchased ".$sitename." for $".$sitecost." in the Site-Trader game.";
       sp_add_to_activitydb($sp_string);
}

Summary

Placing a do_action()  into your code allows other functions to hook into it to extend the functionality of where that hook is placed, using add_action().