How do I pass a variable to create_function() ?
I have something like this:
function my_hook_function(){
$var = 10;
apply_filters('hook', $var);
return $var;
}
$variable = 5;
$function = create_function('', 'return $variable;');
add_filter('hook', $function);
echo my_hook_function();
but it doesn’t work 🙁
theoretically the output should be 5
add_filter() is a wordpress function that allows you to change stuff around 🙂
There are two problems here. The first one is that you need to tell php what parameters get passed to the ‘created’ function, or reference them as a global inside of the body. The second is that you are expecting $var to be modified by the created function, but you are not passing it a reference. the created function simply returns the new variable and you do nothing with it.
Note that if you run this code exactly like this that both of the filters will be added to the ‘hook’ hook.