Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add_action function in wordpress

well im learning to create a wordpress plugin i downloaded one and read the codes, and i saw this i assume 'foo' is the tag where it will add action to..

but what does the array() exactly do?

add_action('foo', array('foo1', 'foo2'));

i looked at http://codex.wordpress.org/Function_Reference/add_action and there is no clear definition about it ..

like image 459
kapitanluffy Avatar asked Aug 30 '10 04:08

kapitanluffy


2 Answers

Right, the first argument is the tag (to which you'll be adding the action), and the second argument specifies the function to call (i.e. your callback).

The second argument takes in a PHP callback, and as such, accepts a number of valid forms. Check this out for all of them :

PHP Callback Pseudo-Types

The type you've shown above is of type 2. The first element of the array specifies a class, and the second element specifies which function of the class you'd like to call.

So with the example you've given above, what that will do is that, whenever the foo() action is called, it will eventually call foo1->foo2() as well.

like image 76
Richard Neil Ilagan Avatar answered Oct 06 '22 00:10

Richard Neil Ilagan


The second argument of the add_action function is the function to be called with the hook.

function hello_header() {
 echo "I'm in the header!"; 
}

add_action('wp_head', 'hello_header');

The usage of an array as the second argument is to pass a objects method rather than just a regular function.

Have a read up on the how the call_user_func works. Should provide some more insight.

http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback(dead link)

like image 26
Stoosh Avatar answered Oct 05 '22 23:10

Stoosh