Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to get an ID of a user who was just activated?


  • William_Oneb
    Participant

    @william_oneb

    Hi, How can I get an id of a user who was just activated on buddypress? The user is not logged in yet so bp_loggedin_user_id() will not work.

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

  • shanebp
    Moderator

    @shanebp

    Try using this hook:

    do_action( 'bp_core_activated_user', $user_id, $key, $user );


    William_Oneb
    Participant

    @william_oneb

    Thanks @shanebp I’ll try to use that hook. Will let you know the results.


    William_Oneb
    Participant

    @william_oneb

    @shanebp here’s how to hook into “do_action( ‘bp_core_activated_user’, $user_id, $key, $user );”

    // define the bp_core_activated_user callback
    function action_bp_core_activated_user( $bp_core_clear_member_count_caches )
    {
    // make action magic happen here…
    };

    // add the action
    add_action( ‘bp_core_activated_user’, ‘action_bp_core_activated_user’, 10, 1 );

    but I still can’t return the user id. Please help.


    shanebp
    Moderator

    @shanebp

    That’s not how you use a hook.
    https://codex.wordpress.org/Function_Reference/do_action

    function william_bp_core_activated_user(  $user_id, $key, $user ) {
         // do something with $user_id
         // there is no return for add_action, only add_filter
    };
    add_action( 'bp_core_activated_user', 'william_bp_core_activated_user', 10, 3 );

    William_Oneb
    Participant

    @william_oneb

    You’re a GENIUS @shanebp!

    It seems to be working fine except that I’m getting these errors:

    Warning: Missing argument 1 for william_bp_core_activated_user() in /…/two/wp-content/plugins/The Kick/TheKick.php on line 24

    Warning: Missing argument 2 for william_bp_core_activated_user() in /…/two/wp-content/plugins/The Kick/TheKick.php on line 24

    Warning: Missing argument 3 for william_bp_core_activated_user() in /…/two/wp-content/plugins/The Kick/TheKick.php on line 24

    Any ideas on how to fix these @shanebp?


    shanebp
    Moderator

    @shanebp

    No, we have no knowledge of how you wrote or load the plugin.


    William_Oneb
    Participant

    @william_oneb

    It’s an extremely simple plugin that just echos the user id of the user who just got activated. I’m going to do something a little complicated with the id later on. I just want it to work first. Here’s the code.

    <?php
    /*
    Plugin Name: A simple plugin
    Plugin URI: http://google.com
    Description: Test
    Version: 1.5
    Author: John Smith
    Author URI: http://URI_Of_The_Plugin_Author
    License: GPL2
    License URI: https://www.gnu.org/licenses/gpl-2.0.html
    Domain Path: /languages
    Text Domain: my-toolset
    */

    function william_bp_core_activated_user( $user_id, $key, $user ) {
    // do something with $user_id
    // there is no return for add_action, only add_filter
    echo $user_id;
    };
    add_action( ‘bp_core_activated_user’, ‘william_bp_core_activated_user’, 10, 3 );
    add_action( ‘bp_after_activate_content’, ‘william_bp_core_activated_user’, 10, 0 );

    It echos the user id of the user who just got activated and so I’m now sure it’s working.

    What could be wrong with my code @shanebp hence the warnings?


    shanebp
    Moderator

    @shanebp

    Please use the code button when sharing code.

    You’re using 2 hooks – one passes arguments, one does not – resulting in the warnings.
    Therefore you need 2 functions.
    But the hook bp_after_activate_content will not expose the user_id value.

    Please read the codex article I provided a link to.


    William_Oneb
    Participant

    @william_oneb

    @shanebp Thanks for spotting the problem with my code. I just got rid of the following line and I was able to get the user id of a user who just got activated without the warnings!

    add_action( 'bp_after_activate_content', 'william_bp_core_activated_user', 10, 0 );

    In fact, according to my logic, that line is unnecessary anyway.

    Thanks so much @shanebp. The codex article was very helpful too.


    William_Oneb
    Participant

    @william_oneb

    @shanebp is it also possible to use the following filter to get user id on account activation as discussed here? Here is the filter:

    $user = apply_filters( 'bp_core_activate_account', bp_core_activate_signup( $_GET['key'] ) );

    If yes how would I go about it?


    Henry Wright
    Moderator

    @henrywright

    @william_oneb technically yes. If bp_core_activate_signup( $key ) is successful then it will return a user ID. So

    function my_f( $user_id ) {
        // You can do something with $user_id here. Note though, the return value of
        // bp_core_activate_signup() may be false. In that case,
        // the value of $user_id here will be false.
        return $user_id;
    }
    add_filter( 'bp_core_activate_account', 'my_f' );

    William_Oneb
    Participant

    @william_oneb

    Thanks @henrywright. Now that we’ve defined the function which is my_f( $user_id ) and returned user id, how can we use the same function to assign the value to a valuable? something like:

    $user_id = my_f();


    Henry Wright
    Moderator

    @henrywright

    That’s usually fine to do but be careful in this case. Our my_f() function is hooked to the filter bp_core_activate_account. That particular filter executes at a given time only. So your statement $user_id = my_f(); is not useful. $user_id will be undefined.


    William_Oneb
    Participant

    @william_oneb

    Thanks again @henrywright I’m executing the following code on account activation but I now get this error “Warning: Missing argument 1 for my_f() …

    Here is the code:

    function my_f( $user_id ) {
        // You can do something with $user_id here. Note though, the return value of
        // bp_core_activate_signup() may be false. In that case,
        // the value of $user_id here will be false.
        return $user_id;
    
    }
    
    add_filter( 'bp_core_activate_account', 'my_f' );
    
    function do_thingsz(){
    	$willie = my_f();
    	echo "The user id is ";
    	echo $willie;
    }
    
    add_action( 'bp_after_activate_content', 'do_thingsz', 10, 0 );

    Henry Wright
    Moderator

    @henrywright

    I suspected that might happen. The problem is here $willie = my_f();. In this particular case you can’t do that. I may have mislead you earlier so I’ve updated my reply here.


    William_Oneb
    Participant

    @william_oneb

    @henrywright, I’m trying to write a function that will return user id on account activation so that I can use the user id somewhere else with this hook: bp_after_activate_content. Something like my previous code which was:

    function my_f( $user_id ) {
        // You can do something with $user_id here. Note though, the return value of
        // bp_core_activate_signup() may be false. In that case,
        // the value of $user_id here will be false.
        return $user_id;
    
    }
    
    add_filter( 'bp_core_activate_account', 'my_f' );
    
    function do_thingsz(){
    	$willie = my_f();
    	echo "The user id is ";
    	echo $willie;
    }
    
    add_action( 'bp_after_activate_content', 'do_thingsz', 10, 0 );

    Do you have more ideas @henrywright. I appreciate @shanebp’s suggestion above but it’s not working the way I want it to (Returning the user id)


    Henry Wright
    Moderator

    @henrywright

    Is the user logged in at this point? If so, you can use bp_loggedin_user_id()


    William_Oneb
    Participant

    @william_oneb

    No @henrywright. At this point, the user has just clicked on the activation link in email.

    @shanebp suggested this function. It’s working but I can’t get it to return an id that I can store in a GLOBAL variable so that I can use it somewhere else on activation page with this hook:

    
    bp_after_activate_content

    Here’s the function that @shanebp suggested.

    function william_bp_core_activated_user(  $user_id, $key, $user ) {
         // do something with $user_id
         // there is no return for add_action, only add_filter
    };
    add_action( 'bp_core_activated_user', 'william_bp_core_activated_user', 10, 3 );

    Henry Wright
    Moderator

    @henrywright

    If the key is part of the URL then you should be able to get that from $_GET["key"] and then query the database for a user ID using that value.


    William_Oneb
    Participant

    @william_oneb

    Thanks for your reply @henrywright. So how would I go about retrieving the activation key? I’d appreciate if you can at least give me a code that just prints the activation key on the activation page and I’ll try to query the database for the user ID.


    Henry Wright
    Moderator

    @henrywright

    OK sure, try this:

    function william_oneb_key() {
        if ( isset( $_GET['key'] ) ) {
            echo $_GET['key'];
        } else {
            echo "No key available.";
        }
    }
    add_action( 'bp_after_activate_content', 'william_oneb_key' );

    William_Oneb
    Participant

    @william_oneb

    Thanks @henrywright The above code did not echo the key but this one did.

    function william_oneb_key() {
          if ( empty( $key ) ) {
          $key = bp_current_action();
          echo "The activation key is ";
          echo $key;
      }
    }
    add_action( 'bp_after_activate_content', 'william_oneb_key' );

    The final bit would be to query the user id using the key that we just obtained. Do you mind helping me with this one too? Probably a code?

Viewing 22 replies - 1 through 22 (of 22 total)
  • The topic ‘How to get an ID of a user who was just activated?’ is closed to new replies.
Skip to toolbar