2013-04-13 42 views
2

卸載鉤子不能正常工作,因爲當我卸載插件時,表格不會被丟棄。請幫我解決一下這個。以下是我正在使用的代碼。請告訴我我錯過了什麼。Wordpress插件卸載不工作

register_uninstall_hook(__FILE__, 'plugin_db_uninstall'); 


function plugin_db_uninstall() { 
    global $wpdb; 

    $table_name = $wpdb->prefix."user_master"; 
    $wpdb->query("DROP TABLE IF EXISTS $table_name"); 

    $table_name = $wpdb->prefix."candidate_master"; 
    $wpdb->query("DROP TABLE IF EXISTS $table_name"); 


} 

全碼

define('TXTFOLDER', plugins_url()."/candidate_section/txtfiles"); 

function candidate_install() { 
    if (!file_exists(TXTFOLDER)) { 
     mkdir(TXTFOLDER, 0777); 
    } 
} 


function candidate_section_create_table() 
{ 

    global $wpdb; 

     $sql = 
     "CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."user_master` 
     (
      `id` bigint(20) NOT NULL AUTO_INCREMENT, 
      `name` varchar(255) NOT NULL, 
        `email` varchar(255) NOT,NULL, 
      PRIMARY KEY (`id`) 
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;"; 
     dbDelta($sql); 

     $sql1 = 
     "CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."candidate_master` 
     (
      `id` bigint(20) NOT NULL AUTO_INCREMENT, 
      `user_id` bigint(20) NOT NULL, 
        `position` varchar(255) NOT NULL, 
        `status` int(11) NOT NULL, 
      PRIMARY KEY (`id`) 
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;"; 

     dbDelta($sql1); 

} 


function plugin_db_uninstall() { 
     global $wpdb; 

     $table_name = $wpdb->prefix."user_master"; 
     $wpdb->query("DROP TABLE IF EXISTS $table_name"); 

     $table_name = $wpdb->prefix."candidate_master"; 
     $wpdb->query("DROP TABLE IF EXISTS $table_name"); 


    } 


if (is_admin()) { 
add_action('admin_menu', 'candidate_menu'); 
register_activation_hook(__FILE__, 'candidate_install'); 
register_activation_hook(__FILE__, 'candidate_section_create_table'); 
register_uninstall_hook(__FILE__, 'plugin_db_uninstall'); 
} 

回答

1

我不看好的原因,但我可以看到兩件事情。一個是你可以從is_admin()內部刪除所有3 register_*_hook,並將它們留在任何條件之外。

PS:爲什麼你有兩個*_activation_hook s?

另一個是使用文件uninstall.php而不是register_uninstall_hook。我沒有找到鏈接,我看到了所討論的鉤子的陷阱,但由於很長一段時間,我只使用uninstall.php方法。

this note in the Codex,但對沒有進一步提到爲什麼給出:

重點放在使用卸載該插件,而不是register_uninstall_hook的「uninstall.php」的方式。

並鏈接到此WordPress的答案:Uninstall, Activate, Deactivate a plugin: typical features & how-to
但在那邊檢查all the results