1

In my addon, I would like to check on activation, if a column of a specific table in the DB already exists to avoid to implement it for each activation.

Something like this :

class MainClassAddon{

     public function __construct(){
         register_activation_hook( __FILE__, array( $this, 'install' ) );
     }

     public function install(){

         if( /*Checking if column c in table wp_t exists*/ ){
             $wpdb->query("ALTER TABLE wp_t ADD c INT(1) NOT NULL DEFAULT 1");
         }
     }
}
new MainClassAddon();
register_uninstall_hook( __FILE__,  array( 'PluginNamespace\MainClassAddon', 'uninstall' ) );
1

I found this solution : here

So...

class MainClassAddon{

     public function __construct(){
         register_activation_hook( __FILE__, array( $this, 'install' ) );
     }

     public function install(){

         $row = $wpdb->get_results(  "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'wp_t' AND column_name = 'c'"  );

         if(empty($row)){
             $wpdb->query("ALTER TABLE wp_t ADD c INT(1) NOT NULL DEFAULT 1");
         }
     }
}
new MainClassAddon();
register_uninstall_hook( __FILE__,  array( 'PluginNamespace\MainClassAddon', 'uninstall' ) );

Your Answer

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

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