I am learning about OOP, and I think I am getting the hang of it. My question is why the author of a wordpress boilerplate plugin wrote the add action function like this
add_action('admin_init', array(&$this, 'admin_init'));
According to the codex I understand the add_action hook, the paramaters to be passed are $tag "The name of the action to which $function_to_add is hooked", and the function that you want hooked $function_to_add.
Well I understand the functions in OOP are methods, and I can see why that may change the syntax, but that is just a vague representation, I want a clear answer why the author uses an array, then uses &$this. I understand why one would use $this->property but not so sure about &$this.
Is it just how you refer to the method? If so I still dont understand the array, why wouldnt it look something like $this->admin_init.
By the way the structure looks kind of like this
class my_plugin_settings {
public function __construct() {
// register actions
add_action('admin_init', array(&$this, 'admin_init'));
}
public function admin_init() {
//Settings here
}
}
&this provides context to the PHP functions under the hood that hook it up to your admin_init function. Check out the declaration of add_action or take a look at http://www.php.net/manual/en/function.call-user-func-array.php.
That's how PHP knows to use the function in that object, not a global-level function
Related
Can someone help me explain what is going on here? I am going through a Wordpress plugin. It has the following constructor function
protected function __construct() {
do_action_ref_array( 'plugin_specific_action_name', array( $this ) );
add_action( 'init', array( $this, 'init' ) );
}
My question is on the first line of the constructor. The action 'plugin_specific_action_name' didn't have any function associated anywhere. The action hook is plugin specific. Since it didn't have any function associated with it, what does it helps the plugin with.
The author has commented "Announce that the class is ready, and pass the object (for advanced use)". Can someone help me what exactly will that be used for? Why do I have to use this in the constructor function? What will be the advanced use cases I can use that for? Any help in clearing this for me will be appreciated
Ok. do_action or do_action_ref_array is just a placeholder. Any custom behavior can be introduced here with help of add_action. If nothing is specified the action will be muted. So it is a place holder. The eye opener for me here is do_action can exist without add_action
I'm going to structure this as best I can. Basically I have a Hook class that works by adding supplied hooks.
An example of this would be:
$this->registry->hook->add('HOOK_NAME', 'CLASS_NAME||METHOD_NAME')
The hook adding, calling, removing functions work great, now the issue resides when I set the hook in a separate method in a completely different class.
An example of this would be when a user logs in.
The path they take is User Controller -> Form Class -> Login Process Function
Now within this login process function, I'd like to set a hook to be called later to end the session. (Would be added as stated above)
The issue seems to be that it gets set but doesn't stay persistently, if that makes sense?
If anyone is interested, this is what the hook add function looks like:
function add($hook, $callback, $params = '') {
//make sure the hook is defined
if (!isset($this->hooks[$hook])) {
$this->hooks[$hook] = array();
}
//add the callback to the hook
$this->hooks[$hook]['callback'] = $callback;
// add the params if supplied
if (!empty($params)) {
$this->hooks[$hook]['params'] = $params;
}
}
Should I be using magic methods __set() and __get() ?
Any help would be appreciated! :)
How can the WP function add_action receive another function as its argument? I know this by itself is not possible in php (to have custom functions receive other functions as arguments). I deliberately broke some code and found that there is actually a native php function call_user_fun_array beneath this (i think),
but how did they make it so that their own function can have a function as its argument?
There is a native PHP function call_user_func() Take a look on this pretty simple example.
function caller($custom_func){
return call_user_func($custom_func);
}
function make_echo(){
echo 'something';
}
caller('make_echo');
I register class methods for actions in my Wordpress plugin. When my method gets called by Wordpress, if I try to use the $this variable, php throws an error saying the call to $this variable is illegal outside the context of an object.
How can that be? I thought unless the method is static, you're not supposed to be able to call class methods if the class isn't instantiated! My method isn't static! What is happening?
The source code
Obviously the initialize is called from the main plugin file:
add_action('init', array('AffiliateMarketting', 'initialize'), 1);
My class looks like this:
class AffiliateMarketting
{
public function __construct()
{
// some initialization code
}
public function initialize()
{
add_action('woocommerce_before_single_product', array("AffiliateMarketting", "handleAffiliateReferral"));
}
public function handleAffiliateReferral($post)
{
$this->xxx(); // <---- offending function call
}
public function xxx()
{
}
}
The received error message is in fact Fatal error: Using $this when not in object context in <filename> on line <linenumber>.
You have to instantiate the class first. Something like this:
$affiliatemarketing = new AffiliateMarketing;
and then do the following:
add_action('init', array(&$affiliatemarketing, 'initialize'), 1);
Edit: forgot to add, your action in your method should be added like this:
add_action('woocommerce_before_single_product', array(&$this, "handleAffiliateReferral"));
You're not supposed to be. That's why you're getting an error.
I don't know exactly how you're registering the method (code would help), but probably, you're expecting Wordpress to take care of creating an instance, but that's not its role.
I found thought the Codex documented if the class name is specified using its string representation, then the add_action function will assume the call is to a static method.
On the other hand if and instance of the class is passed along then add_action will use that instance to make the method call.
Although Arman hasn't specified which php version he is using, I would assume it's probably 5.3.2 or 5.3.3. The error itself is rather similar to the one described in this question and the solution also would be to upgrade to the latest version of php 5.3.
I am trying to create a wordpress plugin, I found one plugin which use oops concepts, my question is why the second parameter in the add_action function is an array instead of a function name
add_action('admin_menu', array(&$this,
'my_menu'));
my_menu is a function in the same class, please help me
Thanks
Because the second argument needs to be a callback. (and add_action internally uses call_user_func_array).
For functions we can just pass its name as a string but we can't do that with object methods, can we?
So an array is passed with 2 elements, first the object and second the method to call:-
array( $object, 'method' )
Oh and you can safely remove that useless '&', PHP4 days are gone now.
#Thomas John, you are correct about second argument in add_action also in wordpress org not mentioned anything about this so now, let me know you, we can pass array as second argument array($this,'method').
Description:
when object creates of class then constructor automatically calls and your action performs.
WHY IT REQUIRES
in wordpress how to create or initialize the class in add_action method in short add_action referencing a class check below example
class Myclass{
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
}
}
Referencing a class using add_action().