After you register a user, do you see a message that starts with You have successfully created your account! ?
If you do, your function should work.
You could try putting it in the theme/functions.php
Putting code directly into a template is okay if you create a template overload.
Yeah I get the message “You have successfully created your account! To begin using this site you will need to activate your account via the email we have just sent to your address.” but nothing gets printed…weird. I’ve tried sticking in an exit; too and nothing.
Does anyone have any additional insight on why I’m not getting anything printed? Thank you in advance.
I just tested it and it works properly. [ Don’t use an ‘exit’. ]
function test() {
echo "<br>test worked!<br>";
}
add_action('bp_after_registration_confirmed', 'test');
Are you using a custom register page?
If so, does it have the do_action hook you’re trying to use?
Also try it with a standard WP theme like 2015.
Could be a function name conflict? Try happymunkee_hook_test()
just to be sure that’s not the issue.
Thanks for your responses. Plugging in the same code into functions.php inside the theme itself seems to resolve the problem. I put the snippet in wp-content/themes/klein/functions.php. Can someone confirm if this code will ever be overwritten by an update to WordPress or BuddyPress? I know updating BP will overwrite the register.php page.
Can someone confirm if this code will ever be overwritten by an update to WordPress or BuddyPress?
Updating WordPress or BuddyPress won’t overwrite anything you have added to your theme, but updating your theme will do. The way around this is to create a Child Theme.
Thank you, Henry. If you don’t mind that I add an additional question, how do I retrieve registration data from the hook? I would like to grab the username, email, display name, and meta data. I know it follows this form:
function test_hook($user_id)
will get me the user’s ID to use. However, does every BP default registration value have its own hook variable? IE. is there a variable for username, email, password, and meta data (custom fields)? Or do I access them some other way?
Take a look at the source where the hook appears. See here. You should see do_action( 'bp_after_registration_confirmed' );
.
This invokes all functions hooked to bp_after_registration_confirmed
. So for example:
function test() {
// The code here will be run.
}
add_action( 'bp_after_registration_confirmed', 'test' );
In this particular case, nothing is passed to test()
. So to answer your question:
how do I retrieve registration data from the hook?
If an argument is supplied to do_action()
like this: do_action( 'bp_after_registration_confirmed', $user_id );
, then that argument is passed to your custom function. You can receive that argument like this:
function test( $user_id ) {
// Do something with $user_id here.
}
add_action( 'bp_after_registration_confirmed', 'test' );
So the short answer is if you want to use $user_id
in your custom function, then bp_after_registration_confirmed
isn’t the right hook to use, because it comes with no arguments. This isn’t always the case because sometimes you may have request variables available but I won’t get into that.
Hope this helps!