1

I wanted to explore more options and thats the reason for me building my first WP Plugin. I tryed to follow the coding standards, but I am struggeling hard right now. I am trying to drop my created database tables, when the uninstall hook is triggered. I am trying for 6 Hours now, using google, youtube, stackoverflow and more, but not getting any working result for me.

When I activate my plugin, I create 2 tables in my database. I want to delete these two tables, while uninstall this plugin.

My code:

// create uninstall hook

register_uninstall_hook( __FILE__ , 'uninstall_function' );

function uninstall_function() {
    global $wpdb;

    $table_name = $wpdb->prefix . "one_of_two_tables";

    $sql_delete = "DROP TABLE IF EXISTS $table_name";

    $wpdb->query( $sql_delete );
}

I also tryed this (found on stackoverflow):

function delete_plugin_database_tables(){
    global $wpdb;
    $tableArray = [   
      $wpdb->prefix . "table_name1",
      $wpdb->prefix . "table_name2",
   ];

  foreach ($tableArray as $tablename) {
     $wpdb->query("DROP TABLE IF EXISTS $tablename");
  }
}

This code is inside of the plugin main file, not in the uninstall.php and it is wrapped by the activation function (hook). I dont know anymore.. any help :D?

Update! So just to let you know: Its working with an uninstall.php. Not the way I wanted to do it, but its working for now. If someone does know why my uninstall hook is not working, just let me know. Otherwise I use the uninstall.php for now on.

8
  • Where is this being added register_uninstall_hook()? If it's a separate file, are you sure you're including that file into your main plugin? – Howdy_McGee Apr 13 at 17:57
  • Can you not use uninstall.php? If the plugin is not active then there's no way to register the uninstall hook. Is it that the table isn't deleted, or that the hook does not run? – Tom J Nowell Apr 13 at 18:10
  • Hey @Howdy_McGee -> no its the main file. So i just have one php file right now. – Mauric3 Apr 13 at 18:55
  • Hey @Tom J Nowell -> Why should I use an other file, for just deleting 2 tables? – Mauric3 Apr 13 at 19:07
  • Can you answer my other question, I asked wether the problem is that the uninstall hook does not run, or that it does run but the table is not dropped. – Tom J Nowell Apr 13 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.