• ian0mackenzie

    (@ian0mackenzie)


    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.

Viewing 1 replies (of 1 total)
  • RossMitchell

    (@rossmitchell)

    When the plugin did this:
    add_action( 'wp_head', array( &$this, 'addMetaData' ) );
    It was already in a particular instance of some class, an instance of a class is called an object, as an implementation detail it has an address, at this address are tables of data relating to the class, and with the values which are specific to that particular object.

    The registration is saying, to the list of functions associated with ‘wp_head’ add this call. It being an array the interpretation is that there is some object of some class with the address &$this, upon this object invoke the function addMetaData. The crucial thing is that it is a call upon this particular object, not another one of the same type.

    So what you have found is that the function visual_composer() returns the address of this very same object. So now the remove_action function can identify just what has to be removed from the ‘wp_head’ action list, namely this function call upon this object.

    Lets create a more tangible example.
    Say we are creating paginated text for a book, in the process when pages are full a footer function is called to finish the page, it outputs footnotes, page number and chapter number etc. It also has to count the pages. So we have a class called page_count (with member functions like add_one, set_start, get_value etc), it is extended and creates several instances: $book_count and $chapter_count.
    Among the actions registered in various places we have:

    add_action( ‘pg_end’, array( &$book_count, ‘add_one’ ) );
    add_action( ‘pg_end’, array( &$chapter_count, ‘add_one’ ) );

    Functions creating the page header and footer use these counters for page numbering, and page within chapter labels. These are also handled by action hooks like.

    add_action( ‘pg_label_hdr’, array( &$book_count, ‘get_label’ ) );
    add_action( ‘pg_label_hdr’, array( &$chapter_count, ‘get_label’ ) );

    Now after all the chapters are created, we progress to the index, but its pages are labeled differently. So we must revoke the chapter counting hooks (index counting hooks will be added too), so these revoke calls would be:

    remove_action( ‘pg_label_hdr’, array( &$chapter_count, ‘get_label’ ) );
    remove_action( ‘pg_end’, array( &$chapter_count, ‘add_one’ ) );

    Note that we need to stop doing the chapter related stuff, but have to keep doing the book page stuff, so when I remove the action I have to identify both the function and the object.

    Hope this helps.

Viewing 1 replies (of 1 total)
  • The topic ‘remove_action on a method: Why didn't this work?’ is closed to new replies.