This tutorial shows you how to recover from a WordPress white screen of death in the future. If you’re looking at one right now, there’s no shortcut. You need to manually reset your theme, disable your plugins, or both. But if you want to be prepared for next time, and want to recover instantly from a blank screen staring at you when WordPress crashes, then read on.
Table of Contents
Step 1: Insert the Code
Start by adding the following code into your functions.php or custom plugin. If you’re new, follow this tutorial on the proper way to add code to WordPress.
function reset_theme_and_plugins() { if (!current_user_can( 'manage_options' )) return; $url = $_SERVER['REQUEST_URI']; $getFirstPart = explode('/', $url); $getFirstPart = $getFirstPart[1]; if ($getFirstPart == 'reset-theme') { switch_theme('twentysixteen'); wp_redirect( home_url() ); exit; } if ($getFirstPart == 'deactivate-plugins') { $plugins = get_option( 'active_plugins' ); require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); deactivate_plugins($plugins); wp_redirect( home_url() ); exit; } } add_action( 'setup_theme', 'reset_theme_and_plugins' );
This creates two special URLs that only administrators can use.
- www.yourwebsite.com/reset-theme
- www.yourwebsite.com/deactivate-plugins
The first one “www.yoursite.com/reset-theme” automatically sets your theme to the default Twenty Sixteen. It loads well before any error even has a chance to occur. If you want this to default to some other theme instead, change this line:
switch_theme('twentysixteen');
Replace “twentysixteen” with the folder name of the theme you want to revert to whenever you get a fatal error or a WordPress white screen of death.
The second URL “www.yourwebsite.com/deactivate-plugins” immediately deactivates all plugins on your site.
Location of the Above Code
Note that if you paste this code into your theme’s functions.php, you won’t be able to use the second URL after the first. That’s because the theme would have already changed and a new functions.php would be in effect. So if you want to disable plugins while having the above code in functions.php, make sure you run the second URL first!
The reverse is true if you paste the code into a custom plugin. In this case, you need to disable them first using the “reset-theme” URL, and then disable the plugins. If you disable the plugins first, you won’t be able to reset the theme!
In short:
- Code in functions.php => Disable plugins first. Disable theme second
- Code in custom plugin => Disable theme first. disable plugins second.
Of course, if you’re sure of the source of the problem (plugin, or theme), then you can disable whichever one you want 🙂
Example 1: WordPress White Screen of Death Fix
Let’s say I make a syntax error in my WordPress functions.php and I get either a fatal error or a white screen of death like this:
Since I know my error lies in my theme’s functions.php (because I just made a change), I type in the first URL to revert back to my “safe” theme – Twenty Sixteen, by typing in testing.wp-tweaks.com/reset-theme;
As soon as this executes, I’m directed back to my home page with the Twenty Sixteen theme installed.
Everything’s safe. Now I can go back and correct whatever mistake I made.
Example 2: Deactivating all Plugins
Let’s say I get a fatal error or a white screen of death because of an incorrect plugin. In that case, I type in the second URL for deactivating them all: testing.wp-tweaks.com/deactivate-plugins:
Now my WordPress site is restored, and all my plugins are deactivated as shown here:
Using one, or both of these URLs will fix your WordPress white screen of death in seconds. Sure, it won’t fix the underlying problem. The theme or plugin that caused the error still exists and is messed up. But at least now you have the chance to go in and fix it. Otherwise, you’d be locked out of the admin screen entirely.
Keep in mind that the above code will only work when you try these URLs as an admin user. So it won’t work in a fresh incognito window, or if some other visitor to your site attempts to reset your theme or plugins.
As I mentioned earlier, this tutorial won’t help you if you’re currently stuck on a white screen of death. But it has the potential to save you a lot of time and headaches in the future. Just type in a URL and bam – Your problem is fixed!
Speak Your Mind