4

I want to automatically login to WordPress by parsing the username and password fields as parameters into the login URL.

For example, I want to create a link as follows:

www.mysite.com/wp-admin?username=xxxx&pass=xxxxx

Theoretically, if I paste that link into my web browser,

I want to be automatically logged in without having to put data into the form fields however, this does not happen.

Is the a way to do this?

The purpose of this is to use AJAX in a web app to login.

1
  • It is generally a very bad idea to send the password through a query string ($_GET) parameter. When you do this, the password will be saved in the web server's log files, the browser's history (Though not with AJAX) and can risk being sent in the referrer header. In this situation I would create a separate, secret value, that can be used for this purpose. Apr 12, 2022 at 14:01

1 Answer 1

5

Using bellow code in first line of functions.php in your theme or a specific plugin.

<?php
if( isset($_GET['username']) and $_GET['pass'] ) {
    $user = get_user_by('login', $_GET['username']);

    if ( $user && wp_check_password( $_GET['pass'], $user->data->user_pass, $user->ID) ) {
        wp_set_current_user($user->ID, $user->user_login);
        wp_set_auth_cookie($user->ID);
        do_action('wp_login', $user->user_login);

        wp_redirect( admin_url() );
        exit;
    }

    wp_redirect( home_url() );
    exit;
}
?>

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