add_action function in wordpress

24,549

Solution 1

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.

Solution 2

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)

Share:
24,549

Related videos on Youtube

kapitanluffy
Author by

kapitanluffy

Updated on July 23, 2021

Comments

  • kapitanluffy
    kapitanluffy almost 3 years

    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 ..

  • kapitanluffy
    kapitanluffy over 13 years
    thank you richard, i understand it (almost) xD so you mean foo1 is the class and foo2 is the method within the class right? hell, i need to learn oop in php xD thanks guys sorry for such a dumb question :P
  • Stoosh
    Stoosh over 13 years
    @kapitanluffy - can you please mark this question as answered
  • ken
    ken over 11 years
    Is that right that foo2() must be static? Why is that? This effectively means you can't pass into objects only class definitions?