0

I hope this is the right place to post this - if not, please just simply direct me to the right place!

I recently came across a plugin that, among other things, added a meta entry to the header. I needed to remove this action. The add_action entry looked like this:

add_action( 'wp_head', array( &$this, 'addMetaData' ) );

"this" is in relation to the class, called Vc_Base.

My initial reaction was to add the following to my functions.php:

function removeAddMetaData() {
    $vc = new Vc_Base();
    remove_action('wp_head', array(&$vc, 'addMetaData'),10000);
}
add_action('init', 'removeAddMetaData', 10000);

This did not work. I also tried changing 10000 to other values.

What DID end up working was this:

function removeAddMetaData() {
    remove_action('wp_head', array(visual_composer(), 'addMetaData'));
}
add_action('init', 'removeAddMetaData');

I tracked down visual_composer() and it only has one line:

return vc_manager()->vc();

I tracked down vc_manager()->vc() and, among a few other things, it contains these lines:

$vc = new Vc_Base();
$this->factory['vc'] = $vc;
return $this->factory['vc'];

So, if that's just doing what I did (in a round-about way), why didn't my initial idea work? I'd really appreciate some feedback since I feel I'll understand WordPress/PHP a whole lot more if I can understand this!

Thanks in advance.

1
  • Milo's answer is spot-on and I want to add some more specific details for anyone else. In looking closer at vc_manager()->vc(), there is a check to see if $this->factory['vc'] is defined. If it is, then that same object is returned which is why this works: remove_action('wp_head', array(visual_composer(), 'addMetaData')); That said, this also works: global $vc_manager; remove_action('wp_head', array($vc_manager->vc(), 'addMetaData')); The trick is to make sure that same instance of Vc_Base is returned. Thanks for the answer! Commented Nov 26, 2014 at 16:54

1 Answer 1

1

Your first method doesn't work because you create a new instance of the class and try to remove the action from that new instance. That new instance you created is not the same instance that the action was added to, so it doesn't work.

In the second example, you get the original instance that was created by the plugin, so removing the action from that instance succeeds.

Have a look at this question for some other ways classes may instantiate themselves in plugins.

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.