How to execute a funciton while user activation in WPMU

Hi

I've wrote a function in mu-plugins folder. I need to execute that function while the user is activating either from his activation mail or from admin. If I call that function manually and passing some user_id it is working fine. but it's not working while the user is activating.

my code is as follows:

<?php
function store_users($user_id) {
global $wpdb;
$user = get_user_by( 'id', $user_id );
$sponsor = get_user_meta($user_id, 'affiliate_referred_by', true);
$userid = $user->ID;
$userpass = $user->user_pass;
$wpdb->insert( 'sample_table', array('userid'=>$userid, 'password'=>$userpass, 'sponsor'=>$sponsor) );
}
add_action('wpmu_activate_user', 'store_users', 10);
?>

  • Ash
    • WordPress Hacker

    Hello there

    I hope you are well today and thanks for asking the question.

    Please try the following code:

    <?php
    function store_users($user_id, $password, $meta) {
        global $wpdb;
        $user = get_user_by( 'id', $user_id );
        echo "";
        print_r($user);
        echo "";
        $sponsor = get_user_meta($user_id, 'affiliate_referred_by', true);
        $wpdb->insert( 'sample_table', array('userid'=>$user_id, 'password'=>$password, 'sponsor'=>$sponsor) );
    }
    add_action('wpmu_activate_user', 'store_users', 10, 3);
    ?>

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Srinivas Peesapaty
    • Design Lord, Child of Thor

    Hi Ashok,
    I've tried as per your reply but unable to get data in sample_table after user activation from Activation mail same as previous.
    my filename is store_users.php and located in wp-content/mu-plugins/store_users.php

    You've used $user_id, $password, and $meta in function but we're not using $meta and $password anywhere. Because for getting password I'm using $user object as below in function
    $userpass = $user->user_pass;

    And $meta is not using anywhere in the function. So I think no use of $password and $meta, right?

  • Ash
    • WordPress Hacker

    Hello @Srinivas Peesapaty

    As the do_action provides 3 params, so it's better to receive 3 params as function arguments. Though you can use the number of params that you need.

    Just a question, are you creating the users from front end or back end?

    Cheers
    Ash

  • Ash
    • WordPress Hacker

    Hi there

    Please try the following hook:

    wpmu_activate_blog

    You will find it in ms-functions.php line no 1070:

    do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta );

    User_ID is 2nd parameter here, so you need to pass at least 2 params to get the user id.

    Let me know if it works.

    Cheers
    Ash

  • Ash
    • WordPress Hacker

    Hi there

    I can see you are using buddypress. When site is registered via buddypress, those hooks are not fired.

    BuddyPress uses wpmu_signup_blog() function to create blog and you will notice there is no hook.

    And activation mail users are getting is not subsite activation, it's user buddypress activation.

    So, in this case, user_registration or bp_core_signup_user would be good option.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.