Make WordPress Core

Opened 8 years ago

Closed 6 years ago

#39225 closed defect (bug) (duplicate)

Callbacks with a higher priority within the same hook no longer get triggred since 4.7

Reported by: miunosoft's profile miunosoft Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7
Component: Plugins Keywords:
Focuses: Cc:

Description (last modified by desrosj)

Hi,

It seems callback functions registered with a higher priority than a currently executed callback with the same hook do not get called.

The below is a demonstration plugin that illustrates the problem.

<?php
/**
 *  Plugin Name: Test - Hooks Behaviour 47 vs 4.6.x or below
 *  Description: Tests the hooks behaviour to make comparision between WordPress 4.7 and 4.6.x or below.
 *  Version:     1.0
*/

class TestActionHookBehaviour {

    private $_sHookName = '';
    private $_sLogFileName = '';
    private $_sCallID = '';

    public function __construct( $sHookName ) {

        $this->_sHookName    = $sHookName;
        $this->_sLogFileName = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . basename( get_class() ) . '_' . date( "Ymd" ) . '.log';
        $this->_sCallID      = uniqid();

        add_action( $this->_sHookName, array( $this, 'doAction' ), 10 );
        do_action( $this->_sHookName );

    }

    public function doAction() {

        $this->_log( __METHOD__ . ' ' . current_filter() );

        // Register a callback with the same action while in the action with a higher priority.
        add_action( $this->_sHookName, array( $this, 'doWithinSameAction' ), 1 );

    }

    /**
     * This method is called in v4.6.x or below but not in v4.7.
     */
    public function doWithinSameAction() {
        $this->_log( __METHOD__ . ' called.' );
    }

    private function _log( $sMessage ) {
        file_put_contents(
            $this->_sLogFileName, // file name
            $this->_sCallID . ' ' . $sMessage . PHP_EOL,
            FILE_APPEND
        );
    }

}
new TestActionHookBehaviour( 'my_custom_action' );

If you run this in WordPress 4.7, the doWithinSameAction() method never gets called while it does in WordPress 4.6 or below.

Change History (2)

#1 @ocean90
8 years ago

Sounds like a duplicate of #38011, assuming that higher means earlier, which I think is true here.

#2 @desrosj
6 years ago

  • Description modified (diff)
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #38011.

Going to close this out since there has been no further feedback on this.

Note: See TracTickets for help on using tickets.