• Resolved nikksan

    (@nikksan)


    Hey Im trying to execute code that saves users 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

    (@tsquez)

    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

    I apologize for any inconvenience this may cause.

    Thread Starter nikksan

    (@nikksan)

    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

    (@nikksan)

    Ofc you have to create this table first 😀

    Theme Author ThemeAWESOME

    (@tsquez)

    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.