0

I'm trying to pass a argument to the function. But it is not transmitted. var_dump produces

string'' (length = 0)

What am I doing wrong?

$map_vars = 'Test';

function map_data($data) {
    var_dump($data);
}

add_action('wp_footer', 'map_data', 10, 1);
do_action( 'map_data', $map_vars );
0

The footer doesn’t run in the global context, so the variable $map_vars is not in the correct scope.

To keep the variable and the callback in the same scope, you could use a class:

class Map_Var_Example
{
    public static $map_vars;

    public static function map_data()
    {
        var_dump( self::$map_vars );
    }
}

Map_Var_Example::$map_vars = 'Test';

add_action( 'wp_footer', array( 'Map_Var_Example', 'map_data' ), 10, 1 );
0

I think you have misunderstood actions and hooks.

Your code outputs empty string, because what you do is:

  1. Create some function which takes 1 parameter and var_dumps it.
  2. Assign this function as action to wp_footer hook. This hook doesn't have any parameters, so WordPress calls your function, during do_action('wp_footer'); without any parameters.
  3. Then you call your own do_action('map_data') with some parameters, but you have never assigned any function to this hook. It also isn't default WordPress hook, so there is no function assigned to hook map_data. Ergo nothing will happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.