Title: Execute code right after user logs in
Last modified: September 1, 2016

---

# Execute code right after user logs in

 *  Resolved [nikksan](https://wordpress.org/support/users/nikksan/)
 * (@nikksan)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/execute-code-right-after-user-logs-in/)
 * Hey I`m trying to execute code that saves user`s location when he logs in the
   site.
    I tried adding this to wp-forge/function.php
 *     ```
       $cu = wp_get_current_user();
       $login = $cu->login;
   
       add_action( 'wp_login', 'add_user_location', 10, 2 );
       function add_user_location( $login, $cu ) {
          /* code */
       }
       ```
   
 * I used die(); inside the code to check if it executes or not and it doesnt.
    
   Can you help me ?

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

 *  Theme Author [ThemeAWESOME](https://wordpress.org/support/users/tsquez/)
 * (@tsquez)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/execute-code-right-after-user-logs-in/#post-7634691)
 * HI there,
 * Unfortunately, this falls outside the scope of support I offer for WP-Forge. 
   This is a “**how to**” question which I have covered here [https://wordpress.org/support/topic/support-issue-clarification?replies=1](https://wordpress.org/support/topic/support-issue-clarification?replies=1)
 * I apologize for any inconvenience this may cause.
 *  Thread Starter [nikksan](https://wordpress.org/support/users/nikksan/)
 * (@nikksan)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/execute-code-right-after-user-logs-in/#post-7634711)
 * Thanks anyways , I found the solution online.
    Here is the final code , eventually
   you end up with a table containing all logged in users , with their locations
   based on gelocation by IP.
 *     ```
       function addUserLocation($login){
          $user = get_user_by('login',$login);
   
          global  $wpdb;
   
          $loc = json_decode( file_get_contents('http://ipinfo.io/'.$_SERVER['REMOTE_ADDR'].'/json') )->loc;
          $latitude  = explode(',' , $loc)[0];
          $longitude = explode(',' , $loc)[1];
   
          $wpdb->insert(
           'locations',
          array(
            'user_id'   => $user->ID ,
       	 'firstname' => $user->display_name ,
       	 'lastname'  => $user->last_name,
       	 'email'     => $user->user_email,
       	 'latitude'  => $latitude ,
       	 'longitude' => $longitude
          ),
          array(
           '%d' ,
       	'%s' ,
       	'%s' ,
       	'%s' ,
       	'%s' ,
       	'%s'
          )
          );
       }
   
       function removeUserLocation(){
             global $current_user;
             global  $wpdb;
       	  $wpdb->delete( 'locations', array( 'user_id' =>  $current_user->data->ID ), array( '%d' ) );
   
        }
   
       add_action( 'wp_login' , 'addUserLocation', 99 );
       add_action( 'clear_auth_cookie', 'removeUserLocation' , 1);
       ```
   
 *  Thread Starter [nikksan](https://wordpress.org/support/users/nikksan/)
 * (@nikksan)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/execute-code-right-after-user-logs-in/#post-7634712)
 * Ofc you have to create this table first 😀
 *  Theme Author [ThemeAWESOME](https://wordpress.org/support/users/tsquez/)
 * (@tsquez)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/execute-code-right-after-user-logs-in/#post-7634759)
 * Awesome, glad you got it working. I appreciate you posted what you found here,
   it may help someone else in the future.

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

The topic ‘Execute code right after user logs in’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/wp-forge/6.5.3.1/screenshot.png)
 * WP-Forge
 * [Support Threads](https://wordpress.org/support/theme/wp-forge/)
 * [Active Topics](https://wordpress.org/support/theme/wp-forge/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/wp-forge/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/wp-forge/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [ThemeAWESOME](https://wordpress.org/support/users/tsquez/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/execute-code-right-after-user-logs-in/#post-7634759)
 * Status: resolved