admin管理员组文章数量:1405186
Never creates self filters before. In my plugin I have variable as array. I want another users can change this array without plugin modification. I'm trying this code in my plugin.php
:
<?php
/*
Plugin Name: Test plugin
*/
$arr = [
'val',
'val2',
'val3'
];
$arr = apply_filters( 'my_hook', $arr );
print_r( $arr );
In my functions.php
I puts this code:
add_filter( 'my_hook', 'modify', 10, 1 );
function modify( $arr ) {
unset($arr[0]);
return $arr;
}
I expecting output without first element, but it outputs original array with 3 values;
What's wrong with it?
Never creates self filters before. In my plugin I have variable as array. I want another users can change this array without plugin modification. I'm trying this code in my plugin.php
:
<?php
/*
Plugin Name: Test plugin
*/
$arr = [
'val',
'val2',
'val3'
];
$arr = apply_filters( 'my_hook', $arr );
print_r( $arr );
In my functions.php
I puts this code:
add_filter( 'my_hook', 'modify', 10, 1 );
function modify( $arr ) {
unset($arr[0]);
return $arr;
}
I expecting output without first element, but it outputs original array with 3 values;
What's wrong with it?
Share Improve this question edited Dec 24, 2019 at 12:18 user3013494 asked Dec 24, 2019 at 6:48 user3013494user3013494 451 silver badge7 bronze badges 5 |1 Answer
Reset to default 0Thanks for your answers. According to comments above, I modified my code. Now it works. Is it correct way?
<?php
/*
Plugin Name: Test plugin
*/
$arr = [];
add_action( 'init', 'set_var_data' );
function set_var_data() {
global $arr;
$arr = [
'val',
'val2',
'val3'
];
$arr = apply_filters( 'my_hook', $arr );
}
print_r( $arr );
本文标签: filtersCan39t understand applyfilter logic
版权声明:本文标题:filters - Can't understand apply_filter logic 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744881238a2630219.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
function.php
but the file in the theme isfunctions.php
with a "s". – Kaperto Commented Dec 24, 2019 at 8:50globals
, in this case it's the only way to solve my problem? – user3013494 Commented Dec 24, 2019 at 13:23