I'm adding data to a table whenever a new user registers on the site.

add_action( 'user_register', 'auto_follow_admin', 10, 1 );

function auto_follow_admin() {
    global $wpdb;
    $wpdb->insert( 'wp_um_followers', array( 
    'time' => current_time( 'mysql' ),
    'user_id1' => 1, 
    'user_id2' => $user_id 

Everything works fine apart from the the new $user_id which isn't being passed to the query.

The stored data after it's run looks like this :

time : 2015-08-27 14:08:32
user_id1 : 1
user_id2 : 

Is global $wpdb; resetting the user_id?


Because you need to give your function a parameter:

function auto_follow_admin( $user_id ) {

    // Now you can use $user_id, which is passed to the function from the hook caller

  • Ahh I feel stupid now.. How did I miss that! Thanks man ;) – Grant Aug 27 '15 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.