I am new to WP, semi-new to PHP and new to OOP.

1) How can the WP function add_action receive another function as its argument? I know this by itself is not possible. I deliberately broke some code and found that there is actually a native php function call_user_fun_array,

but how did they make it so that their own function can have a function as its argument?

2) I am doing some plugin tutorial and working with WP_Query, and I just passed an argument directly to WP_Query, as if it was a method and not a class.

I did the following: 

$loop = new WP_Query(
            'posts_per_page' => 3

I would understand if I first instantiated the class and THEN used its method, something like:

$loop = new WP_Query();
            'posts_per_page' => 3,
            'category__in' => $cats,
            'orderby' => 'rand',
            'post__not_in' => array($id)

but how is this possible, that instantiating a class can act as a method as well? (this is more of a general OOP question I guess).

1 Answer 1

  1. add_action/add_filter recive few arguments. Second one is callback.
  2. It's done using constructor method.
  • I understand the second answer but not the first. OK it is a callback, but it is still a function, and it seems to be the basis for this is the native php function i already mentioned, but I do not understand how it is done. Nov 6, 2013 at 19:12
  • You don't understand what is callable data type? Nov 6, 2013 at 20:05
  • If you need help understanding basic PHP datatypes and processes, perhaps this is not the best place to learn them. Consider a PHP Tutorial or the generic programming stackexchange instead.
    – Otto
    Nov 6, 2013 at 20:15
  • As I said in my post, I said I didn't understand how can a custom function take another function as its argument. Nov 6, 2013 at 21:19
  • call_user_func_array isn't a custom function. Nov 6, 2013 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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