I'm working on some function that can accept argument as any array. Some value in this array is defined as variable. This function is class method defined in another file. Is there any way to replace that variable in array by variable defined in method?
The main goal is to have ability to pass $args
array with different structure, but there always be a variable $filter_value
as value of one array key.
I want to tell method in some way to replace that vaiable with dynamic value that will be passed as $_GET
parametr.
So now method accept all args, create array from it, push in global property as array element. And then runing run()
method tring to merge two arrays, one of them is passed to method, and there should be placed value insted of $filter_value
variable
test.php
$authors_arr = array(
'5' => 'author_name',
'7' => 'author_name2',
);
$args = array('author'=>$filter_value);
$dynamic_admin = new DynamicAdmin();
$dynamic_admin->addFilter( 'post', 'author', __('All authors','fn'), $authors_arr, $args);
$dynamic_admin->run();
class.php
class DynamicAdmin{
private $filter_fn = array();
public function addFilter($post_type, $meta_key, $label, $value_list, $args){
$args_arr = compact('post_type', 'meta_key', 'label', 'value_list','args');
$this->filter_fn[] = $args_arr;
}
public function run(){
foreach ($this->filter_fn as $filter){
$filter_value = $_GET[$filter['meta_key']];
$query->query_vars = array_merge($query->query_vars, $filter['args']);
}
}
};
For example in current case $filter['meta_key'] = 'author'
, in my URL there ?auhor=5
and after run()
method i want to get $query->query_vars
looks like:
Array(
...,
'author' => 5,
...,
)