0

I have a problem when I want to get user id WordPress after login in file wp_login.php, I tried to insert some code in this file, after line wp_safe_redirect($redirect_to); in login case.

I must get user id after login to update my custom WordPress table.

session_start();
require( dirname(__FILE__) . '/wp-load.php' );
global $wpdb;

$session_id     = session_id(); 
$current_user   = wp_get_current_user();
$id_user        = $current_user->ID;

$wpdb->query( 
    $wpdb->prepare("UPDATE wp_shortlist SET id_user='$id_user' WHERE id_session='$session_id' ")
);

any idea to get user id after login in file wp_login.php?

3
  • You have the biggest security hole there. Care to tell me your web address? Seriously, don't modify the core files!
    – Twifty
    Sep 19, 2013 at 5:54
  • you are editing in the defoult core wordpress files and it may affect to your site and also when you update you version all the changes will lost so try to work only in your custom theme Sep 19, 2013 at 6:01
  • yes i know that if i change my core files, maybe i can get security hole but i'm very confused how to get user id after succesfull login . can i hook action after user login like after_setup_theme via function.php ? Sep 19, 2013 at 6:05

1 Answer 1

1

As said above modifying the core files is a bad idea, by doing so you are opening up security holes and on top of all this you loose the changes made on up-gradation.

To do the above you can try the wp_login hook, put the codes in your active theme's functions.php

function after_login() {
    $current_user = wp_get_current_user();
    $id_user = $current_user->ID;
       // The query you were trying would go here.
}
add_action( 'wp_login', 'after_login' );

Hope it helps !

3
  • still can't get user id :( Sep 20, 2013 at 6:38
  • Can You please check if the query you are writing is right, please check the syntax too. Try and run the query in the phpmyadmin. Sep 20, 2013 at 6:58
  • yes, i have checked my query and also try and run in phpmyadmin. the problem is i can't get current user id after login . Sep 20, 2013 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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