My plugin creates a folder in wordpress upload folder called my_plugin_folder.Now when I click uninstall the plugin the folder should be deleted. I tried the solutions given here but its not working. https://stackoverflow.com/questions/1334398/how-to-delete-a-folder-with-contents-using-php
1 Answer
In WP you have 2 solutions for code to be excecuted during your plugin uninstall process.
First solution
In your plugin's root folder, create the file uninstall.php
. This file will be loaded automatically by WP during uninstall process. Still, to prevent direct access, you need to make sure we're in the uninstall process for example by checking the WP_UNINSTALL_PLUGIN
global.
// If uninstall.php is not called by WordPress, die
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
die;
}
delete_folders();
// ...
Second solution
In your main plugin file, use register_uninstall_hook
.
register_uninstall_hook( __FILE__, 'plugin_uninstall' );
function plugin_uninstall() {
delete_folders();
// ...
}