How to check if tables in Wordpress still exists after activations

I have problems that my checkIfTablesExists() function is never called, even though I add some fake table name to the list of table names.

The reason I want to make this function is to check if the table still after update for example. It should also make sure that the unknown php sever allows for table creation in the database.

plugin.php

function activate_myplugin()
{
    require_once PLUGIN_PATH. 'classes/class-plugin-activator.php';
    Plugin_Activator::activate();
    Plugin_Activator::checkIfTablesExists();
}
register_activation_hook(__FILE__, 'activate_myplugin');

plugin-activator.php

?php

class Plugin_Activator
{
    protected static $_instance = null;

    public function __construct()
    {

    }

    public static function instance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    // Fired during plugin activation
    public static function activate()
    {
        Plugin_Activator::create_database_tables();
    }

    public static function create_database_tables()
    {
        // This creates the tables
    }

    public static function checkIfTablesExists()
    {
        global $wpdb;
        
        $table_prefix = $wpdb-prefix . 'myplugin_';
        $tables = [
            $table_prefix . 'myplugin_table1',
            $table_prefix . 'myplugin_table2',
            $table_prefix . 'myplugin_table3',
        ];
        foreach ($tables as $table_name) {
            if ($wpdb-get_var(SHOW TABLES LIKE '$table_name') != $table_name) {
                add_action(admin_notices, array(Plugin_Activator::instance(), notifyTablesDostExists));
            }
        }
    }

    // TODO: Is not called successfully
    public function notifyTablesDostExists()
    {
        echo 'div class=error noticep' . __('Essential tables for My Plugin are missing.', 'myplugin') . '/p/div';
    }
}

Topic activation database plugins Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.