admin管理员组文章数量:1293914
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 notice"><p>' . __('Essential tables for My Plugin are missing.', 'myplugin') . '</p></div>';
}
}
本文标签: pluginsHow to check if tables in Wordpress still exists after activations
版权声明:本文标题:plugins - How to check if tables in Wordpress still exists after activations 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741588775a2386997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论