apply_filters – with anonymous function
-
I wanted to pass along this tip, ( I’m new to PHP and WordPress so if this is obvious, sorry ?? )
I needed to create a get_option filter for a bunch of uniquely named option names, so for instance, option_key1, option_key2, option_key3 etc.. I don’t have control over when get_option is called but I do need to modify the information that is returned ( by combining it with data from a secondary datasource ).
There is no way for me to know at the time of writing the script what all of the keys will be. In the processing function that I’m creating, I need to know the value of the option_keyX so I can take some different actions based on a few different factors.
Here is what I’ve come up with ( note I DO know what all of the key names will be at runtime )foreach($optionkeys as $key){ add_filter( 'option_'.$key, create_function('$value',' return supplement_option_value($value,"'.$key.'");'), 10, 1); }
Then just create your supplement_option_value function and you will have both the option value that was retrieved, and the key for that option.
- The topic ‘apply_filters – with anonymous function’ is closed to new replies.