Creating a Plugin Uninstall Confirmation Dialog

Posted on 19th June 2023

Uninstalling a WordPress plugin is a simple process. You can do it from the plugin page in the WordPress admin area. However, when you uninstall a plugin, there is no confirmation dialog asking you if you’re sure you want to do it. This can lead to accidental uninstalls, which can be a pain to recover from.

In this article, we will show you how to create a plugin uninstall confirmation dialog in WordPress.

Why You Need a Confirmation Dialog

When you delete a file from your computer, most operating systems will ask you to confirm that you want to delete the file. This is because deleting a file is a permanent action and there is no way to recover it once it’s been deleted.

The same is true for uninstalling a WordPress plugin. Once a plugin is uninstalled, all of its data is deleted from your WordPress database. This can lead to data loss if you accidentally uninstall a plugin.

A confirmation dialog will help to prevent accidental plugin uninstalls. It will also give users a chance to change their minds if they didn’t mean to uninstall the plugin.

Creating the Plugin Uninstall Confirmation Dialog

First, you need to create a new file in your WordPress plugin and name it uninstall.php. You can add this file to the root of your plugin or inside a sub-folder.

Once you have created the file, you need to add the following code to it. This code will create the uninstall confirmation dialog.

if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

if ( ! current_user_can( 'activate_plugins' ) ) {
	return;
}

$plugin_name = 'Plugin Name';

check_admin_referer( "uninstall-plugin_{$plugin_name}" );

$options = get_option( 'plugin_name_options' );

if ( ! isset( $options['remove_data_on_uninstall'] ) || ! $options['remove_data_on_uninstall'] ) {
	return;
}

// Delete plugin options from the database.
delete_option( 'plugin_name_options' );

// Delete user meta data.
delete_metadata( 'user', 0, 'plugin_name_user_meta', '', true );

// Delete posts and post meta data.
$posts = get_posts( array(
	'post_type'   => 'plugin_name_post_type',
	'post_status' => 'any',
	'numberposts' => -1,
	'fields'      => 'ids',
) );

foreach ( $posts as $post_id ) {
	wp_delete_post( $post_id, true );
}

Let’s take a look at this code and see what it’s doing.

  • if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
    }

This line of code is important. It tells WordPress that this file is only meant to be run when a plugin is uninstalled. If this file is run for any other reason, it will simply exit.

  • if ( ! current_user_can( 'activate_plugins' ) ) {
    return;
    }

This line of code checks to see if the current user has the ‘activate_plugins’ capability. This capability is only given to administrators. If the current user doesn’t have this capability, the code will stop running.

  • $plugin_name = 'Plugin Name';

This line of code sets the $plugin_name variable to the name of your plugin. You need to change ‘Plugin Name’ to the actual name of your plugin.

  • check_admin_referer( "uninstall-plugin_{$plugin_name}" );

This line of code checks the nonce for the uninstall process. A nonce is a security measure that helps to prevent malicious uninstall requests. If the nonce check fails, the code will stop running.

  • $options = get_option( 'plugin_name_options' );

This line of code gets the plugin options from the WordPress database. These options are stored in the plugin_name_options option.

  • if ( ! isset( $options['remove_data_on_uninstall'] ) || ! $options['remove_data_on_uninstall'] ) {
    return;
    }

This line of code checks to see if the remove_data_on_uninstall option is set to true. This option is set by the user on the plugin settings page. If this option is not set or is set to false, the code will stop running.

  • // Delete plugin options from the database.
    delete_option( 'plugin_name_options' );

This line of code deletes the plugin options from the WordPress database. These options are stored in the plugin_name_options option.

  • // Delete user meta data.
    delete_metadata( 'user', 0, 'plugin_name_user_meta', '', true );

This line of code deletes the user meta data for your plugin. This data is stored in the plugin_name_user_meta option.

  • // Delete posts and post meta data.
    $posts = get_posts( array(
    'post_type' => 'plugin_name_post_type',
    'post_status' => 'any',
    'numberposts' => -1,
    'fields' => 'ids',
    ) );

    foreach ( $posts as $post_id ) {
    wp_delete_post( $post_id, true );
    }

This line of code deletes all of the posts and post meta data for your plugin. The posts are deleted using the wp_delete_post() function. This function will also delete the post meta data for each post.

Conclusion

In this article, we showed you how to create a plugin uninstall confirmation dialog in WordPress. This confirmation dialog will help to prevent accidental plugin uninstalls.

Now that we have a functioning plugin, let’s add an uninstall confirmation dialog to it. This is a good idea because it gives the user a chance to change their mind before deleting the plugin’s data.

To do this, we’ll need to use the WordPress API’s register_uninstall_hook() function. This function takes two arguments: the path to the plugin file and a callback function. The callback function will be called when the user tries to uninstall the plugin.

In our callback function, we’ll use the WordPress API’s wp_die() function to display a confirmation message. wp_die() takes two arguments: a message and a title. The message will be displayed to the user and the title will be used as the title of the confirmation dialog.

If the user clicks “OK” on the dialog, the plugin will be uninstalled. If they click “Cancel”, the plugin will remain installed.

Here’s the code for our uninstall confirmation dialog:

<?php

// Callback function for the 'register_uninstall_hook' function
function my_plugin_uninstall_confirm($plugin) {
// Check that the user has the required capability
if (current_user_can('delete_plugins')) {
// Display a confirmation message
$confirm = __('Are you sure you want to delete this plugin?', 'my-plugin');
$confirm_delete = __('Deleting this plugin will also delete all of its data. This cannot be undone.', 'my-plugin');
wp_die($confirm . '

‘ . $confirm_delete, __(‘Confirm Plugin Uninstall’, ‘my-plugin’), array(‘back_link’ => true));
}
}

// Register the uninstall hook
register_uninstall_hook(__FILE__, ‘my_plugin_uninstall_confirm’);

?>