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.

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

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

    $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?

  • You have the biggest security hole there. Care to tell me your web address? Seriously, don't modify the core files! – Twifty Sep 19 '13 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 – Vikas Gautam Sep 19 '13 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 ? – Ardi Wirawan Sep 19 '13 at 6:05

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 !

  • still can't get user id :( – Ardi Wirawan Sep 20 '13 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. – Maruti Mohanty Sep 20 '13 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 . – Ardi Wirawan Sep 20 '13 at 7:07

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.