{"id":1200,"date":"2019-05-19T14:56:52","date_gmt":"2019-05-19T17:56:52","guid":{"rendered":"https:\/\/felipeelia.com.br\/?p=1200"},"modified":"2019-05-19T15:20:34","modified_gmt":"2019-05-19T18:20:34","slug":"wordpress-recovery-mode-functions-filters-and-settings","status":"publish","type":"post","link":"https:\/\/felipeelia.dev\/wordpress-recovery-mode-functions-filters-and-settings\/","title":{"rendered":"WordPress Recovery Mode: Functions, Filters, and Settings"},"content":{"rendered":"\n
While studying for my most recent video about the new resources for developers in WordPress 5.2 (in Brazilian Portuguese)<\/a>, I had to write a code to store the link sent via email by the new WordPress Recovery Mode<\/a>.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n 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<\/em> \u2014 a plugin that can’t be deactivated. To create a plugin like that you have to put the PHP file in the mu-plugins<\/em> folder, inside wp-content<\/em>. It’ll be at the same level of plugins<\/em> and themes<\/em> directories and, if it doesn’t exist yet, you’ll have to create it.<\/p>\n\n\n\n Worth mentioning the mu-plugins<\/em> work in a bit different way from normal plugins: the PHP files are kept in the directory root. Must use plugins<\/em> inside subfolders won’t be recognized.<\/strong> That said, I’ve created a file named as store-recovery-keys.php<\/em>, put the content above and saved. After that, the access link is saved in the recovery-links.php<\/em> file, inside the wp-content\/uploads<\/em> folder.<\/p>\n\n\n\n WordPress 5.2 allows you to change the email address which will receive the Recovery Mode link in two different ways:<\/p>\n\n\n\n The first way, the simpler one, is setting the It’s also possible to change the Recovery Mode email address through the recovery_mode_email<\/em> filter:<\/p>\n\n\n\n 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<\/em> filter. I suggest you put this code in a separate plugin. If the code is inside your theme’s functions.php<\/em> file and your theme is paused due to an error, the code will be ignored.<\/p>\n\n\n\n You can use the time constants provided by WordPress. I’ve already written about them in this post<\/a> (in pt_BR yet).<\/p>\n\n\n\n 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? \ud83d\ude42<\/p>\n\n\n\n With the wp_is_recovery_mode()<\/a> 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.<\/p>\n\n\n\n Having this constant set in your wp-config.php<\/em> 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.<\/p>\n\n\n\n Drop-ins are a type of WordPress extension with predefined names that are kept in the wp-content<\/em> folder. Probably, advanced-cache.php<\/em> is the most famous one.<\/p>\n\n\n With the new drop-ins, you can change the error message displayed (php-error.php<\/em>) or change the way Wordpress will handle errors (fatal-error-handler.php<\/em>). To use the latter you have to extend the Remember drop-ins<\/em> that change something displayed in the screen, like php-error.php<\/em> and db-error.php<\/em>, 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.<\/p>\n\n\n\n If you want to disable this resource completely, you can use the It’s useful for hosting companies, for example, who handle errors in a custom different way.<\/p>\n\n\n\n It’s possible to check if the WordPress Recovery Mode is available with the wp_is_fatal_error_handler_enabled()<\/a> function. In other words, this function is useful to check if Recovery Mode wasn’t deactivated through the If you still need help with something related to WordPress, give a look at my Codeable Profile<\/a> and click on the Hire button<\/a>!<\/p>\n\n\n\n Do you want to help me? Share this content in your social networks! \ud83d\ude42 Also, if you noticed something wrong, feel free to contact me<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" Read more about the new functions, filters, and settings for the WordPress Recovery Mode included in WP 5.2.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[91],"tags":[],"yoast_head":"\n<?php\n\/*\n * Plugin name: Store Recovery Keys\n *\/\n\nadd_action(\n 'generate_recovery_mode_key',\n function( $token, $key ) {\n require_once( ABSPATH . 'wp-admin\/includes\/file.php' );\n global $wp_filesystem;\n\n WP_Filesystem();\n\n $upload_dir = wp_upload_dir();\n\n $php = '<?php \/\/ ' . add_query_arg(\n\t\t\tarray(\n\t\t\t\t'action' => 'enter_recovery_mode',\n\t\t\t\t'rm_token' => $token,\n\t\t\t\t'rm_key' => $key,\n\t\t\t),\n\t\t\twp_login_url()\n\t\t) . ' ?>';\n\n $wp_filesystem->put_contents( trailingslashit( $upload_dir['basedir'] ) . 'recovery-links.php', $php, 0644 );\n },\n 10,\n 2\n);<\/code><\/pre>\n\n\n\n
How to change the WordPress Recovery Mode email address<\/h2>\n\n\n\n
The RECOVERY_MODE_EMAIL Constant<\/h3>\n\n\n\n
RECOVERY_MODE_EMAIL<\/code> constant inside your wp-config.php. Before the line containing
\/* That's all, stop editing! Happy publishing. *\/<\/code>, you can insert something like this:<\/p>\n\n\n\n
define( 'RECOVERY_MODE_EMAIL', 'email@domain.com' );<\/code><\/pre>\n\n\n\n
The recovery_mode_email<\/em> filter<\/h3>\n\n\n\n
add_filter(\n 'recovery_mode_email',\n function( $email_data ) {\n $email_data['to'] = 'user@example.com';\n return $email_data;\n }\n);<\/code><\/pre>\n\n\n\n
How to change the interval between Recovery Mode<\/em> emails<\/h2>\n\n\n\n
add_filter(\n 'recovery_mode_email_rate_limit',\n function( $interval ) {\n return 12 * DAY_IN_SECONDS;\n }\n);<\/code><\/pre>\n\n\n\n
Other functions and setting available<\/h2>\n\n\n\n
wp_is_recovery_mode()<\/h3>\n\n\n\n
WP_RECOVERY_MODE_SESSION_ID<\/h3>\n\n\n\n
php-error.php<\/em> and fatal-error-handler.php<\/em> drop-ins<\/h3>\n\n\n\n
WP_Fatal_Error_Handler<\/code> class and return an instance of your class at the end of the file.<\/p>\n\n\n
How to completely deactivate WordPress Recovery Mode<\/h3>\n\n\n\n
WP_DISABLE_FATAL_ERROR_HANDLER<\/code> constant. You have to set it in your wp-config.php<\/em>:<\/p>\n\n\n\n
define( 'WP_DISABLE_FATAL_ERROR_HANDLER', true );<\/code><\/pre>\n\n\n\n
Check if Recovery Mode is available<\/h3>\n\n\n\n
WP_DISABLE_FATAL_ERROR_HANDLER<\/code> constant. It is very different from
wp_is_recovery_mode()<\/code>, that’s good for checking if the Recovery Mode is being executed in that exact moment.<\/p>\n\n\n\n
\n\n\n\n