Forums

obtaining current username in a plugin (2 posts)

  1. jorge-silva
    Member
    Posted 1 year ago #

    I'm trying to write a very simple plugin to track when users log in to wordpress. At the moment, it's not going well :-(

    I can't understand why, but the below doesn't work. Any ideas why?

    function access_log_insert_record()
    {
    	global $current_user;
    	$cur_user = $current_user->user_login;
    	global $wpdb;
    	$table = $wpdb->prefix."access_log";
    	$wpdb->query("INSERT INTO $table (access_user, access_date_time) VALUES($cur_user, NOW()) ");
    
    }
    
    add_action('wp_login', 'access_log_insert_record');
  2. Matiss
    Member
    Posted 1 year ago #

    At this points $current_user is not created yet. at least that, what it looks like. you can get user_login directly from wp_login action like this:

    function access_log_insert_record($login)
    {
    	global $wpdb;
    	$table = $wpdb->prefix."access_log";
    	$wpdb->query("INSERT INTO $table (access_user, access_date_time) VALUES($login, NOW()) ");
    
    }
    
    add_action('wp_login', 'access_log_insert_record');

    At least, thats how i got user thats loggin in from wp_login hook.

Topic Closed

This topic has been closed to new replies.

About this Topic