我安装了一个插件,用于加载阵列的元盒($requirements
) 编辑帖子中的项目列表。
当我print_r
插件的数组显示:
Array
(
[item1] => Array
(
[status] => 1
[label] => Excerpt has text
[value] => 1
[rule] => only_display
[type] => simple
)
[item2] => Array
(
[status] =>
[label] => Between 100 and 600 words
[value] => Array
(
[0] => 100
[1] => 600
)
[rule] => block
[type] => counter
)
)
我想在函数中更改数组并替换为我自己的数组。php关于编辑帖子。
插件的类类似于:
class MY_Checklist extends MY_Module
{
/**
* List of requirements, filled with instances of requirement classes.
* The list is indexed by post types.
*
* @var array
*/
protected $requirements = [];
/**
* List of post types which supports checklist
*
* @var array
*/
protected $post_types = [];
/**
* Instace for the module
*
* @var stdClass
*/
public $module;
/**
* Construct the MY_Checklist class
*/
public function __construct()
{
//.....
}
public function display_meta_box($post)
{
$requirements = []; //Array I want to change with my own
$requirements = apply_filters(\'my_checklist_requirement_list\', $requirements, $post) // . <--- Array I want to change with my own
}
}
这是我在我的函数中得到的。php和我不确定我在做什么。
add_action( \'load-edit.php\', \'change_checklist_array\' );
function change_checklist_array() {
class MY_NEW_Checklist extends MY_Checklist {
function display_meta_box( $post ) {
$my_new_array = "...";
}
}
}
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
好的,那么您正在尝试创建自己的类,该类将从plugin扩展该类。问题是插件不会使用您的类,因此您必须修改插件。
另一方面,如果您仔细观察该类:
public function display_meta_box($post)
{
$requirements = []; //Array I want to change with my own
$requirements = apply_filters(\'my_checklist_requirement_list\', $requirements, $post) // . <--- Array I want to change with my own
}
你会看到,有
my_checklist_requirement_list
过滤器在那里,你可以使用它。
所以你所需要做的就是把它放到你的函数中。php:
add_filter( \'my_checklist_requirement_list\', function( $requirements, $post ) {
$requirements = array( ... );
return $requirements;
}, 10, 2 );