@toscho实现得很好。已测试且正确。这是一个稍加修改的版本,它还传递了$句柄和$object\\u名称,因此您只能在需要时进行筛选。
class Filterable_Scripts extends WP_Scripts
{
function localize( $handle, $object_name, $l10n )
{
$l10n = apply_filters( \'script_l10n\', $l10n, $handle, $object_name );
return parent::localize($handle, $object_name, $l10n);
}
}
add_action( \'init\', function() {
$GLOBALS[\'wp_scripts\'] = new Filterable_Scripts;
});
add_filter(\'script_l10n\', \'se108362_example_filter\', 10 , 3);
// Example
function se108362_example_filter($l10n, $handle, $object_name ) {
if(\'js-handle\' == $handle && \'jsVariable\' == $object_name) {
return \'Something Else\';
}
return $l10n;
}