WordPress password reset

Recently I had to update a site but didn't have an admin account or access to the SQL database. So I wrote this short script to reset a forgotten password.

To use it, edit it and fill in the proper values. Then upload it to the wp-content/mu-plugins/ directory, create the directory if necessary.

The code will run automatically, there's no need to activate it like a plugin, but you have to delete the file after using it.

The script may also fail if there is no user with an ID of 1.

<?php
$user_id = 1; # Insert your user ID or simply 1 for the first user that was created
$user_pass = 'secret'; # Insert a new password
$ip = '127.0.0.1'; # Insert your IP

if ($_SERVER['REMOTE_ADDR'] === $ip) {
        require_once(ABSPATH . WPINC . '/registration.php');
        require_once(ABSPATH . WPINC . '/pluggable.php');
        $user = wp_update_user(array(
                'ID' => $user_id,
                'user_pass' => $user_pass,
        )) ;
        var_dump(get_userdata($user));
        die('disable me!');
}

Published on Oct. 18, 2012 at 3:03 p.m. by Nicolas and tagged WordPress, password. You can follow the discussion with the comment feed for this post. Feeling generous? Donate!

5 comments

  1. avatar
    wrote this comment on
    Hello,
    The web developer no longer exists and when I try to renew the password to gain access to work on my site,I received a key to click on:

    "http://[edit]/login/?action=rp&key=c4xyVcPuD9Z4MTWs8llF&login=malcolm,"

    however the following is the reply from the log in page:

    "Sorry, that key does not appear to be valid."

    How can I renew the password and gain access to my website and please note that I am by far an expert at computer work.

    Your soonest reply would be much appreciated
    Malcolm
    Reply to this comment
    1. avatar
      wrote this comment on
      Is this the built-in reset form? You should probably try again, maybe it's just expired. The script I posted here could also be used, I thought the instructions were enough.. maybe not. I'm also available for hire, please see my contact page.
      Reply to this comment
  2. avatar
    wrote this comment on
    Hi, A few questions:
    1) How to I actually get it to run? Will it run automatically by going to my site?
    2) I'm not using multiuser, so I presume I put the file in wp-content/plugins/ instead of mu-plugins?
    3) Does the file have to have any specific name?
    Reply to this comment
    1. avatar
      wrote this comment on
      1) Yes
      2) No
      3) No

      See also http://codex.wordpress.org/Must_Use_Plugins
      Reply to this comment
  3. avatar
    wrote this comment on
    I was editing my wordpress site and login details. I forgot to write down the info before closing the window and I worked for quite a while now have no access because I dont remember the login in username or password. neither do I have access to the email. I urgently need help. Please advise.
    Reply to this comment

Start a new thread

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.