WordPress Recovery Mode: Functions, Filters, and Settings

While studying for my most recent video about the new resources for developers in WordPress 5.2 (in Brazilian Portuguese), I had to write a code to store the link sent via email by the new WordPress Recovery Mode.

Thanks to the new Fatal Error Handler resource, when your theme or a plugin throws an error, WordPress sends an email to the site administrator. This email contains an access link to the brand new Recovery Mode, but one of the URL parameters is encrypted before being stored in the database, i.e., this information is only available in the email. If you don’t receive the email you won’t be able to access the Recovery Mode.

On my local tests, even with an SMTP plugin set, the email wasn’t delivered. So, I’ve created the plugin below, saved him as a must use plugin — a plugin that can’t be deactivated. To create a plugin like that you have to put the PHP file in the mu-plugins folder, inside wp-content. It’ll be at the same level of plugins and themes directories and, if it doesn’t exist yet, you’ll have to create it.

<?php
/*
 * Plugin name: Store Recovery Keys
 */

add_action(
    'generate_recovery_mode_key',
    function( $token, $key ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        global $wp_filesystem;

        WP_Filesystem();

        $upload_dir = wp_upload_dir();

        $php = '<?php // ' . add_query_arg(
			array(
				'action'   => 'enter_recovery_mode',
				'rm_token' => $token,
				'rm_key'   => $key,
			),
			wp_login_url()
		) . ' ?>';

        $wp_filesystem->put_contents( trailingslashit( $upload_dir['basedir'] ) . 'recovery-links.php', $php, 0644 );
    },
    10,
    2
);

Worth mentioning the mu-plugins work in a bit different way from normal plugins: the PHP files are kept in the directory root. Must use plugins inside subfolders won’t be recognized. That said, I’ve created a file named as store-recovery-keys.php, put the content above and saved. After that, the access link is saved in the recovery-links.php file, inside the wp-content/uploads folder.

How to change the WordPress Recovery Mode email address

WordPress 5.2 allows you to change the email address which will receive the Recovery Mode link in two different ways:

The RECOVERY_MODE_EMAIL Constant

The first way, the simpler one, is setting the RECOVERY_MODE_EMAIL constant inside your wp-config.php. Before the line containing /* That's all, stop editing! Happy publishing. */, you can insert something like this:

define( 'RECOVERY_MODE_EMAIL', '[email protected]' );

The recovery_mode_email filter

It’s also possible to change the Recovery Mode email address through the recovery_mode_email filter:

add_filter(
    'recovery_mode_email',
    function( $email_data ) {
        $email_data['to'] = '[email protected]';
        return $email_data;
    }
);

How to change the interval between Recovery Mode emails

By default, WordPress Recovery Mode waits a whole day before sending another email. You can change this interval using the recovery_mode_email_rate_limit filter. I suggest you put this code in a separate plugin. If the code is inside your theme’s functions.php file and your theme is paused due to an error, the code will be ignored.

add_filter(
    'recovery_mode_email_rate_limit',
    function( $interval ) {
        return 12 * DAY_IN_SECONDS;
    }
);

You can use the time constants provided by WordPress. I’ve already written about them in this post (in pt_BR yet).

Other functions and setting available

The new resource brings a lot of options and useful functions. I’ve spoken about them in the video but I’ll list them here as well. Some SEO won’t hurt, right? 🙂

wp_is_recovery_mode()

With the wp_is_recovery_mode() function it’s possible to test if the Recovery Mode is running or not. It can be useful to show a message or perhaps make your plugin behave differently during a Recovery Mode session.

WP_RECOVERY_MODE_SESSION_ID

Having this constant set in your wp-config.php will force the Recovery Mode. In other words, if it’s not wrapped by a test, every user will always be in the Recovery Mode. Use carefully.

php-error.php and fatal-error-handler.php drop-ins

Drop-ins are a type of WordPress extension with predefined names that are kept in the wp-content folder. Probably, advanced-cache.php is the most famous one.

With the new drop-ins, you can change the error message displayed (php-error.php) or change the way Wordpress will handle errors (fatal-error-handler.php). To use the latter you have to extend the WP_Fatal_Error_Handler class and return an instance of your class at the end of the file.

Remember drop-ins that change something displayed in the screen, like php-error.php and db-error.php, aren’t connected to the theme. WordPress possibly didn’t get to the point of including your theme during the execution. Have that in mind while defining screen elements, i.e., don’t count on anything of your theme.

How to completely deactivate WordPress Recovery Mode

If you want to disable this resource completely, you can use the WP_DISABLE_FATAL_ERROR_HANDLER constant. You have to set it in your wp-config.php:

define( 'WP_DISABLE_FATAL_ERROR_HANDLER', true );

It’s useful for hosting companies, for example, who handle errors in a custom different way.

Check if Recovery Mode is available

It’s possible to check if the WordPress Recovery Mode is available with the wp_is_fatal_error_handler_enabled() function. In other words, this function is useful to check if Recovery Mode wasn’t deactivated through the WP_DISABLE_FATAL_ERROR_HANDLER constant. It is very different from wp_is_recovery_mode(), that’s good for checking if the Recovery Mode is being executed in that exact moment.


If you still need help with something related to WordPress, give a look at my Codeable Profile and click on the Hire button!

Do you want to help me? Share this content in your social networks! 🙂 Also, if you noticed something wrong, feel free to contact me.

Felipe Elia

Associate Director of Platform Engineering at 10up, WordPress Core Contributor, Global Polyglots Mentor in WordPress.org, and Locale Manager in the Brazilian WP Community.