[wp-trac] [WordPress Trac] #19986: Issue with add_filter() and add_action() when using anonymous functions.
WordPress Trac
wp-trac at lists.automattic.com
Tue Feb 7 23:44:50 UTC 2012
#19986: Issue with add_filter() and add_action() when using anonymous functions.
--------------------------+-----------------------------
Reporter: pagesimplify | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Plugins | Version: 3.3.1
Severity: normal | Keywords: has-patch
--------------------------+-----------------------------
Referencing File: /wp-includes/plugin.php :: function add_filter()
{{{
function add_filter($tag, $function_to_add, $priority = 10, $accepted_args
= 1) {
global $wp_filter, $merged_filters;
$idx = _wp_filter_build_unique_id($tag, $function_to_add,
$priority);
$wp_filter[$tag][$priority][$idx] = array('function' =>
$function_to_add, 'accepted_args' => $accepted_args);
unset( $merged_filters[ $tag ] );
return true;
}
}}}
When registering a function with add_filter() and add_action(), a unique
id ($idx) is generated to be used as an array key in:
{{{
$wp_filter[$tag][$priority][$idx]
}}}
The function that creates this unique key ($idx) returns the function name
passed into the $function_to_add parameter.
{{{
$idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
}}}
If the $function_to_add parameter is a string, the function
_wp_filter_build_unique_id() returns the original function name.
{{{
function _wp_filter_build_unique_id($tag, $function, $priority) {
global $wp_filter;
static $filter_id_count = 0;
if ( is_string($function) )
return $function;
...
}
}}}
Returning a string value for the unique id is a problem when the function
is an anonymous function generated using create_function(). The reason is
anonymous functions are named as "special character + lambda_XX" or as
"ºlambda_XX" where the first character cannot be displayed.
See attached file: anonymous_function_name.jpg
Since array keys can only be integers or strings, the following array is
invalid and causes inconsistent behaviors.
{{{
$wp_filter[$tag][$priority]['ºlambda_XX']
}}}
Although I've seen instances where this will still work, I've also
observed strange issues with inconsistent results where plugins should
work and sometimes do not. One example is with Otto's Simple Facebook
Connect plugin.
See file: sfc-widgets.php
{{{
add_action('widgets_init', create_function('', 'return
register_widget("SFC_Fan_Box_Widget");'));
}}}
I have not been able to isolate the conditions that allow this to work in
one WordPress installation and not in others. However, I have been able to
apply a patch to my installation with consistent and successful results.
See attached patch: plugin.php.diff
While this is an obscure and possibly very isolated issue, it could
resolve unexplained issues related to using anonymous functions with
add_filter() elsewhere.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/19986>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list