Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am updating my plugin for Wordpress version 3.5 compatibility. These are my important plugin files:

Sort_SearchResult_by_Title.php:

<?php
if(!class_exists('Class_Sortsearchresults'))
{
require_once('Class_Sortsearchresults.php');
}
if(!isset($Class_Sortsearchresults))
{
$Class_Sortsearchresults = new Class_Sortsearchresults;
}
?>

Class_Sortsearchresults.php:

<?php
class Class_Sortsearchresults
{
function __construct()
{

    //Register activation hook
       register_activation_hook(__FILE__,array(&$this,'sortresult_install'));

    //Add menu hook
    add_action( 'admin_menu', array(&$this,'sort_searchresults_menu'));

    //Update function hook
    add_action('plugins_loaded', array(&$this,'sortsearchtitle_update_db_check'));

    //Register deactivation hook
    register_deactivation_hook(__FILE__, array(&$this,'sortresult_uninstall'));

    //Run the search function
    add_action('pre_get_posts',array(&$this,'sort_searchresult_by_title'));

}

function sortresult_uninstall() 
{

    //This function is not working or executed

    global $wpdb;       
    $sortsearchtitle_db_version = '1.0';
    $table = $wpdb->prefix."sortsearchresult";
    $wpdb->query("DROP TABLE IF EXISTS $table");
    delete_option('sortsearchtitle_db_version');
}

function sortresult_install()
{
    //install function
}   

function sort_searchresults_menu()
{

    //function for creating menu

}

function sortsearchtitle_update_db_check()
{

    //function for update

    }

function settings_page()
{
    //function for settings form
}

function sortingguide($opt_val)
{

    //sorting options

}

function sort_searchresult_by_title($k)
{
    //function to run customized search

}

}
?>

It appears I have everything working well except the uninstall function. Any clues why this is not working? Thanks.

share|improve this question
    
add comment

1 Answer

up vote 3 down vote accepted

register_deactivation_hook() is launched when a plugin is deactivated. For plugin uninstall, there is register_uninstall_hook().

Wordpress plugins can also have a file called uninstall.php that is ran whenever the plugin is uninstalled. To prevent the uninstall.php file from being triggered directly, the state of a constant called WP_UNINSTALL_PLUGIN should be checked before launching the codes for uninstall.

Your uninstall.php could be:

<?php
//if uninstall not called from WordPress exit
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
    exit ();

global $wpdb;       
$sortsearchtitle_db_version = '1.0';
$table = $wpdb->prefix."sortsearchresult";
$wpdb->query("DROP TABLE IF EXISTS $table");
delete_option('sortsearchtitle_db_version');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.