8

I'm a newbie at PHP and WordPress, so please be gentle!

I'm building a plugin - following a few tutorials, getting it all working.

My question is why is the function I want to call wrapped up in the array($this, function)? For example I have this constructor code inside my class

 function __construct() {
      add_action( 'admin_init',array( $this, 'getStuffDone' ) );
 }

The getStuffDone function gets fired OK - but why does it have to be in the format it is in?

Any insight is much appreciated.

  • 1
    I don't know enough about OOP to give an informative answer but some research gives a few good references: Using add_action with Class and PHP Passing to Functions – Howdy_McGee Apr 16 '15 at 16:54
  • Never register callbacks in constructors. That makes your code untestable. – fuxia Apr 16 '15 at 18:52
  • @toscho link to an example correct initialization setup? Call a dedicated init() function or something from the constructor? – lkraav Apr 17 '15 at 18:00
  • @lkraav See gmazzap's answer to that question. – fuxia Apr 17 '15 at 19:05
5

It's a PHP callback. You need the syntax to keep a reference to the class instance.

Put it this way - if you didn't have $this, how does the caller know that getStuffDone is a method of your class, and not just a regular PHP function? It doesn't.

Using array( $this, 'getStuffDone' ) says to PHP:

Hey bro, you need to call the method getStuffDone on this instance of my class

  • What I don't understand, which may be how I'm reading it, but the first sentence under Passing says except language constructs such as: array(), but here we are passing an array. Is this a typo or am I reading the sentence wrong? – Howdy_McGee Apr 16 '15 at 20:40
  • No it's right, but it's referring to functions, not methods. Read the para directly below it. – TheDeadMedic Apr 17 '15 at 8:17
5

the add_action() function definition looks like:

 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
     return add_filter($tag, $function_to_add, $priority, $accepted_args);
 }

so according to that it looks like it expects (string,string,int,int)

In a traditional php file the second parameter would simply be one of your function names.

so you would have add_action('admin_init','my_init_function')

It looks like you're using a class to encapsulate your plugin. Other classes could have functions in them with the same name (getStuffDone).

So your function is only known with reference to your class, that's why you have to specify your class $this as well as the function name.

If you're trying to reference a function inside a class you have to use array callable syntax

See also codex.wordpress.org

1

See the documentation, under the "User Contributed Notes":

"To use add_action() when your plugin or theme is built using classes, you need to use the array callable syntax. You would pass the function to add_action() as an array, with $this as the first element, then the name of the class method ..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.