I want to do something like this:
function activate_account_callback($user_id) {
function footer_callback() {
// Do some stuff with $user_id
};
add_action('wp_footer', footer_callback);
};
add_action('bp_core_activate_account', 'activate_account_callback');
ie., on the bp_core_activate_account
event I want to attach an event to wp_footer
. The problem I’m having is that the inner function footer_callback
doesn’t seem to be able to access the $user_id
argument passed to activate_account_callback
(I’m not a PHP programmer and I’m not entirely sure how PHP scoping works).
So what I’d like to do is somehow create a new function that takes no arguments but that has access to the $user_id
variable.
So the way I’d like to solve the problem boils down to wanting to do this:
function func1($arg) {
echo($arg);
}
function func2 = some_magic($func1, 3);
func2(); // Should echo "3"
Then I could use the func2
function and pass it to the inner add_action
. I just don’t know what the some_magic
should be.
Or if this an inherently dumb way of doing things I’m open to other suggestions.
Calling a function within a function like that isn’t very standard, and you don’t need to do it that way anyway. Is the activated user also the user who is logged in to WordPress? It would save some effort if you could just use
wp_get_current_user()
.If you need to carry the $user_id from one function to another, between hooks, you can’t transfer it directly and the scope of the functions won’t work for you. You could use globals, or use a function with a static variable.
Using a global variable
(Update) Using a static variable
We’ll call the target twice this way, the first time will be used just to capture the user ID. Once captured, future calls will already have the value. The only real reason you want this over a global variable is if you don’t need the user ID anywhere else.
Notes:
$bp
(which exists, but I do not know if it contains the ID).