In it’s simplest form.
You create a filter function that will return your select values. You name both the function and the hook used to call that function.
Let’s say that you want to use the function named ‘my_function_name’
function my_function_name($choices, $args=array()) {
// generate choices
return $choices.
}
A note about why I chose 'label' => value'
pairs instead of 'value' => 'label'
. There are times when you want to have multiple labels that have the same value. This cannot be done if the value is the array index (key) since only one index of a value can exist in an array. So if we did this
$choices = array(
'1' => 'First Label for Value 1',
'1' => 'Second Label for Value 1'
);
Then there would be only one choice with the value of 1, the second one, since the first index of ‘1’ is overwritten by the second index of ‘1’. However this works perfectly
$choices = array(
'First Label for Value 1' => '1',
'Second Label for Value 1' => '1'
);
Now let’s move on to the hook. You name the hook that calls your function. I realize that this may be a complex notion for those that are only used to adding functions to hooks that exist in WP and are added by plugin devs so that you can create filters and actions for the hooks. Why did I do this. Let’s say that you have 10 different select fields. If I chose the hook name, instead of providing a field where you just enter a custom hook I would instead need to sent your filter function the name of the field and then you’d need to make decisions based on the field name on what you wanted to do. From my point of view this is easier, the hook is called, one function is run and I do not need to do any complex if/elseif/elseif
in my function based on the field name. I know I can just generate the values. This reduces the overall code that needs to be written and the complexity of that code.
So let’s say that you want to use a the hook ‘my-hook-name’, you need at call the WP add_filter() function the same as any other filter, but using your custom hook name.
add_filter('my-hook-name', 'my_function_name', 10, 2);
Now you add the select field you your form and you supply the value of “my-hook-name” when creating the field.
The rest is optional. Let’s say that you want to provide some other value to your filter function that will be used in making decisions about the choices to be returned. You can do this by adding them to the hook name that you add. For example, let’s say that for a particular field what I want is to get all of the post names in a specific category and that category has an term_id of 205. Then in the value of the field I would enter my-hook-name term_id=205
.
This additional value would be passed in the second argument of my function, see the function I posted above
function my_function_name($choices, $args=array()) {
In this particular case $args would be passed and look like this
$args = array(
'term_id' => '205'
);
You can pass as many arguments to your filter as you need to pass and each will become an name => value pair in $args.
I can’t say if any of this has helped.