A
A
apptimeru2016-04-21 00:31:56
PHP
apptimeru, 2016-04-21 00:31:56

How to pass an argument to a callback function?

I have a variable that needs to be passed to a function, but I can't figure out how.
The wordpress documentation says that a variable can be passed in this way:

$var = 1;
function name($var) {echo $var;}
add_action('wp_head', 'name', 10, 1);

What the name function has already done does not see the value of the $var variable, you can of course set the global $var and that's it, but damn it should work, why not?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WP Panda, 2016-04-21
@apptimeru

function filter_name($var){
  if( /*бла бла бла */ ) {
    $var = 1;
  } elseif ( /*бла бла бла */ ) {
    $var = 2;
  } else {
    $var = 3;
  }
  return $var;
}

add_filter('my_name','filter_name');

function name($var = '') {
  echo apply_filters('my_name',$var);
}

add_action('wp_head', 'name', 10, 1);

D
danforth, 2016-04-21
@danforth

$var = 1;
function name($var) {echo $var;}
add_action('wp_head', 'name', 10, 1);
do_action('name', $var);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question