Skip to:
Content
Pages
Categories
Search
Top
Bottom

Custom Hook Help


  • happymunkee
    Participant

    @happymunkee

    Hi all,

    I want to write a hook to capture some user information after a user registers. For some reason, I can’t get it to work. I created a bp-custom.php in wp-content/plugins as directed in documentation. I have a simple function and register a user using the register form to try to get my hook to fire. No dice, the echo doesn’t print.

    
    function test() {
        echo "test worked!";
    }
    add_action('bp_after_registration_confirmed', 'test');
    

    Can someone help me with this? I can achieve the same result I want by adding some code in register.php but I don’t think I want to touch this file, right?

    WP is 4.3.1. BP is 2.3.4

Viewing 9 replies - 1 through 9 (of 9 total)

  • shanebp
    Moderator

    @shanebp

    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.


    happymunkee
    Participant

    @happymunkee

    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.


    happymunkee
    Participant

    @happymunkee

    Does anyone have any additional insight on why I’m not getting anything printed? Thank you in advance.


    shanebp
    Moderator

    @shanebp

    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.


    Henry Wright
    Moderator

    @henrywright

    Could be a function name conflict? Try happymunkee_hook_test() just to be sure that’s not the issue.


    happymunkee
    Participant

    @happymunkee

    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.


    Henry Wright
    Moderator

    @henrywright

    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.


    happymunkee
    Participant

    @happymunkee

    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?


    Henry Wright
    Moderator

    @henrywright

    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!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom Hook Help’ is closed to new replies.
Skip to toolbar