How I Logged Back into WordPress After Locking Myself Out

One problem with doing any task over and over is that your attention tends to wander a bit after the process becomes too familiar.

I noticed this the other day when I was setting up a new WordPress installation. This is something I have done many times before. But this time I somehow got turned around when setting up the options. I managed to change the WordPress URL to point to an invalid URL. (I had intended to change the “Front page displays” option, but got confused. One problem with the WordPress administration screens is that its options do all tend to look similar.)

This was a big problem because WordPress was now convinced that it was actually installed at that invalid URL. Not only did the site no longer display, but WordPress kept trying to route me to that invalid URL whenever I tried to log in. If I couldn’t log in, how could I change the URL back to the correct value?

I could have used phpMyAdmin to fix the site address in the database, but I didn’t know offhand where the value was stored. Besides, I prefer not to change a database directly if I can help it.

However, I remembered an alternative approach I once saw described. In the wp-login.php file, I located this line near the top:

require( dirname(__FILE__) . '/wp-load.php' );

and added these two lines right after it (replacing example.com with my values, of course):

update_option('home', 'http://example.com');
update_option('siteurl', 'http://example.com');

Those lines overrode the incorrect value I had unintentionally added to the WordPress database. With the value overridden, I was able to log back in and correct the URL. I then deleted the extra lines I had added to wp-login.php.

Everything was back to normal, but I will be a little more careful the next time I set up a WordPress instance.

Leave a Reply

Your email address will not be published. Required fields are marked *