[wp-trac] [WordPress Trac] #38011: WP_Hook class late assignment behavior change
WordPress Trac
noreply at wordpress.org
Fri Sep 9 18:33:17 UTC 2016
#38011: WP_Hook class late assignment behavior change
--------------------------+-----------------------------
Reporter: salcode | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Plugins | Version: trunk
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
The recent addition of the `WP_Hook` class has introduced a change in
behavior when a function is added to the currently executing action with
an earlier priority than that which is currently being executed.
'''Example'''
The following code defines a shortcode `[WP_Hook_priority_example]`
{{{#!php
<?php
add_shortcode( 'WP_Hook_priority_example', 'wp_hook_priority_example' );
function wp_hook_priority_example() {
add_action( 'wp_hook_priority_example', 'add_other_functions' );
do_action( 'wp_hook_priority_example' );
}
// This function is called with a priority of 10 on the hook
`wp_hook_priority_example`.
function add_other_functions() {
add_action( 'wp_hook_priority_example', 'print20', 20 );
// Assign print5() to priority 5 (even though priority 5 has
passed already).
add_action( 'wp_hook_priority_example', 'print5', 5 );
}
function print5() { echo '<p>5</p>'; }
function print20() { echo '<p>20</p>'; }
}}}
'''Prior to the Introduction of the WP_Hook Class'''
This code will output
{{{
20
5
}}}
For clarification, I don't think this is a logical behavior but it is the
current behavior in WordPress 4.6 and earlier.
Even though we are executing code assigned to `wp_hook_priority_example`
with '''priority''' `10`, when we assign a function to the same hook using
a priority that has passed (e.g. `5`) the function is still executed.
'''After the Introduction of the WP_Hook Class'''
This same shortcode executed after the introduction of the WP_Hook class
displays only
{{{
20
}}}
I believe this is the more logical behavior, however in terms of backwards
compatibility I believe it is worth considering making this behavior
consistent with the previous versions of WordPress.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/38011>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list