register_uninstall_hook not working on wordpress plugin uninstall
Hi there i make a plugin, now i want that when a user deactivate the plugin, all the functions and posts and option values and meta values also be deleted.
I write a hook and make a file where i delete all the values, but it did not work, i use this hook
register_uninstall_hook('foo_uninstall.php', $callback);
Is there any other hook to use for this purpose.
Here is my foo_uninstall.php code
<?php
global $wpdb;
$del_prefix = $wpdb->prefix;
//if uninstall not called from WordPress exit
if(!defined('WP_UNINSTALL_PLUGIN'))
exit();
//=========> Delete foo Options
delete_option('foo_theme_directory');
delete_option('foo_plugin_slug');
delete_option('foo_article_qty');
delete_option('foo_search_setting');
delete_option('foo_breadcrumbs_setting');
delete_option('foo_sidebar_setting');
delete_option('foo_comments_setting');
delete_option('foo_bgcolor');
delete_option('widget_foo_article_widget');
delete_option('foo_cat_children');
//=========> Get and Delete Foo Page
$foo_get_page = $wpdb->get_results("SELECT * FROM ".$del_prefix."posts WHERE post_type='page' And
post_content = '[foo_article]' And post_name = 'article'",ARRAY_A );
$fooPageID = $foo_get_page['ID'];
$wpdb->query("DELETE FROM ".$del_prefix."postmeta WHERE post_id = '$fooPageID'");
$wpdb->query("DELETE FROM ".$del_prefix."posts WHERE post_title Like '%article%' And post_type =
'page'");
//=========> Delete foo Terms and Taxonomies
$foo_Find_tax = $wpdb->get_results("SELECT term_taxonomy_id FROM ".$del_prefix."term_taxonomy WHERE
taxonomy='foo_article'",ARRAY_A );
$fooTaxID = $foo_Find_tax[0]['term_taxonomy_id'];
foreach($foo_Find_tax as $foo_find_tax){
$fooTaxID = $foo_find_tax['term_taxonomy_id'];
$delete_foo_relations = $wpdb->query("DELETE FROM ".$del_prefix."term_relationships WHERE
term_taxonomy_id='$fooTaxID'");
$delete_foo_tax = $wpdb->query("DELETE FROM ".$del_prefix."term_taxonomy WHERE
term_taxonomy_id='$fooTaxID'");
$delete_foo_terms = $wpdb->query("DELETE FROM ".$del_prefix."terms WHERE term_id='$fooTaxID'");
}
//=========> Delete terms_order From wp_terms
$wpdb->query("ALTER TABLE ".$del_prefix."terms DROP `terms_order`");
//=========> Delete comments of articles
$foo_comments = $wpdb->get_results("SELECT ID FROM ".$del_prefix."posts WHERE post_type =
'foo_article'");
$foo_comment_id = $foo_comments->ID;
foreach($foo_comments as $foo_comment){
$foo_comment_id = $foo_comment->ID;
$wpdb->query("DELETE FROM ".$del_prefix."comments WHERE comment_post_ID='$foo_comment_id'");
}
//=========> Delete All Articles and Attachments
$foo_all_articles = new WP_Query('post_type=foo_article&posts_per_page=-1');
if($foo_all_articles){
while($foo_all_articles->have_posts()) :
$foo_all_articles->the_post();
$fooID = get_the_id();
$foo_del_args = array('post_parent' => $fooID);
$foo_attachments = get_children($foo_del_args);
if($foo_attachments){
foreach($foo_attachments as $foo_attachment){
wp_delete_attachment($foo_attachment->ID, true);
}
}
wp_delete_post($fooID, true);
endwhile;
}
//=========> Remove all files and images
unlink(get_template_directory()."/foo_articles.php");
unlink(get_template_directory()."/foo_style.css");
unlink(get_template_directory()."/single-foo_articles.php");
unlink(get_template_directory()."/taxonomy-foo_cat.php");
unlink(get_template_directory()."/foo_search.php");
?>
Solutions
Remove the part
if(!defined('WP_UNINSTALL_PLUGIN'))
exit();
According to the Plugin Handbook,
WP_UNINSTALL_PLUGIN
is not defined when uninstall is performed by register_uninstall_hook().
try register deactivation hook instead of register uninstall hook . Please do refer here for more.
register_uninstall_hook( __FILE__, 'foo_on_uninstall' );
function foo_on_uninstall() {
// write your uninstall code
}
Similar questions
WordPress plugin activation, deactivation and uninstall hook not being triggered
I am developing a WordPress plugin which requires certain rules to be added to the .htaccess file of the WordPress installation upon activation of the plugin. Initially, before I switched my code to OOP (with classes and functions) the hooks and this functionality was working. But after I switched it, it doesn't work. The above code is how I am try...
uninstall.php does not appear to trigger when uninstalling my plugin
i am new to plugin development in wordpress and i have have this simple test plugin that i am working on. The problem i am having is that the uninstall.php file does not appear to get trigger and as such the database entried i wish to remove remain after the plugin in uninstalled from the WordPress admin. Plugin code: Uninstall.php UPDATE: I have t...
Does plugin uninstall always put WordPress back into original state?
What is a plugin lifetime within a deployed instance of WordPress? Namely:
Wordpress plugin options need to delete after deactivate & uninstall
I have developed a wordpress plugin and I need to delete the options I am creating when uninstalling. What I did is I created a file called uninstall.php and included the following code: And included this in my main plugin file: This is how i'm registering the option in my main plugin file: When I deactivated and deleted the plugin from the dashboa...
Wordpress plugin uninstall.php
I am trying to write a simple plugin that, among other things, creates a custom post type. I have been searching everywhere but I have really no idea on how to write the uninstall function that delete the custom post type and all its entries from the db. I have found this on the WordPress codex, but I don't understand what to do with it.. shame Can...
Wordpress blank screen after uninstall plugin/ deleting post
Using wordpress, but after some time use, when i deactivate/ativate plugin or delete post, then white screen with text "Error is getting display". After enabling the Debug, getting below message Please help for constructors WP_Widget is since version 4.3.0 of date! Use instead __construct () , in /var/www/vhosts/briefmich.de/httpdocs/wp-includes/fu...