Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-Populate wp-login.php Username

Tags:

php

wordpress

I have a WP site whereby i send customers an email with a link to /my-quotes/ this then, has the following code:

<?
if (!is_user_logged_in()): ?>
<meta http-equiv="refresh" content="0;URL=<? echo wp_login_url(get_permalink()); ?>">
<? endif; ?>

What i want to do is, give the link in the email like: /my-quotes/?login=myusername

and that 'myusername' is the login fields value to save the user re-typing it.

I've had a look in wp-login.php and cant work out where to populate the login fields using $_GET, can anyone advise on a solution?

Thanks in advance.

like image 894
Tom Smedley Avatar asked Sep 16 '25 10:09

Tom Smedley


1 Answers

You should do it by hooking in to the login_form action, rather than modifying wp-login.php, which will get overwritten at next update.

You can add this code to your theme's functions.php file:

// As part of WP login form construction, call our function
add_filter ( 'login_form', 'login_form_prepop' );

function login_form_prepop(){
    // Output jQuery to pre-fill the box

    if ( isset( $_REQUEST['myusername'] ) ) { // Make sure a username was passed
?>
<!-- Small jQuery script to set the value of the input field to your variable -->

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function(){
    $("#user_login").val( '<?php echo( $_REQUEST['myusername'] ); ?>' );
});
</script>
<?php
    }
}

Then update your code to include the variable:

<?
if (!is_user_logged_in()): ?>
<meta http-equiv="refresh" content="0;URL=<? echo wp_login_url(get_permalink()); ?>?myusername=username">
<? endif; ?>

If the jQuery footprint is too much, you can achieve the same with JavaScript:

<script type="text/javascript">
var el = document.getElementById("user_login");
el.value = "<?php echo( $_REQUEST['myusername'] ); ?>";
</script>
like image 50
Patrick Moore Avatar answered Sep 19 '25 07:09

Patrick Moore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!