Remembering the username always

[ad_1]

Hey;

One of my most important clients has a requirement where they say that ‘username is not being remembered all the time’. He sends me a screenshot, where his username looks ‘remembered’.

Unfortunately, I’ve tried 3-4 different browsers with EVERY cookie setting known to man, and I can’t get the USERNAME to be remembered, not once, not ever.

I don’t think that is default behavior. So I asked chatGPT

write me a wordpress filter that I can put in functions.php that will remember only the username in a cookie for a wordpress login

// Function to set a cookie with the username upon login

function remember\_username\_cookie() {

// Check if the user is logging in

if (isset($\_POST\[‘log’\]) && isset($\_POST\[‘pwd’\])) {

// Get the username from the login form

$username = $\_POST\[‘log’\];

​

// Set the cookie with the username

setcookie(‘remember\_username’, $username, time() + (30 \* 24 \* 60 \* 60), COOKIEPATH, COOKIE\_DOMAIN);

}

}

add\_action(‘wp\_login’, ‘remember\_username\_cookie’);

​

// Function to pre-fill the username field with the remembered username

function prefill\_username\_field($username) {

// Check if the remember\_username cookie is set

if (isset($\_COOKIE\[‘remember\_username’\])) {

// Get the username from the cookie

$remembered\_username = $\_COOKIE\[‘remember\_username’\];

​

// If the username field is empty, fill it with the remembered username

if (empty($username)) {

$username = $remembered\_username;

}

}

return $username;

}

add\_filter(‘username’, ‘prefill\_username\_field’);

​

Unfortunately, I put this code in my child theme Functions.php. And it’s still not remembering the username.

I’m expecting LOGOUT, and the username still remains. I \*THINK\* that is what the user expects as well.

PS – further complicating things, the username in question has space in it. I’ve never heard of such a style, but I guess it’s allowed. Perhaps for HIS login, I need to URL\_ENCODE and DECODE something?

PPS – instead of adding an action to wp\_login, aren’t I supposed to use wp\_signon (because of ‘secure cookies’ evolution?) I think that I read that somewhere

[ad_2]
1 Comment

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer