0

I have a function that has an add_action inside it that calls the user_register hook. I would like to be able to echo the variable $user_id from the user_register hook and the $_POST array as well. (Ok, my goal is to do more than that, but you get the idea). See below:

function my_function{

add_action( 'user_register' .....

    .
    .
    .
echo $user_id;
var_dump ($_POST);
    .
    .
    .
}

How might I accomplish this?

I realize this is probably really simple but examples that I've seen haven't used an add_action inside another function.

Thanks for any help.

1

2 Answers 2

1

You can't use an action or hook within a function in the way you are expecting. Actions and hooks are about delayed execution You are attaching code that will execute at a later point. After an action is added, your function will continue as normal.

Then when the user is actually being registered, your code will run. You will never get back to your original function though.

You might want to consider using multiple actions, and global variables. In the first action we can set the global:

add_action( 'user_register' .....
global $myvariable;
    .
    .
    .
echo $user_id;
var_dump ($_POST);
$myvariable = 'a Value';
    .
    .
    .
}

Then you can have another action that you fire somewhere else later. Here we can call on this global data:

add_action('wp_footer', function() {
    global $myvariable;
    echo $myvariable;
});
0

When you use add_action your callback function gets put into a kind of a queue and is executed when the action hook runs-- in your case, that would be user_register. Data from the 'hook' is not passed back through add_action. Whatever you need to do, needs to be done in the hook callback or you need to work out another way to do what you need to do and, as you have fatally truncated your code, it is impossible to determine what that is from the question.

Possibly you can do what you need with a static variable, a global, or a class but without more information about the problem you are trying to solve that is about all I can offer.

See: Clarification on filters and hooks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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