jam*_*ton 10 mysql wordpress uninstall
在插件激活时,我有4个在WordPress数据库中创建的表.
在卸载时我想删除表.
我已经能够写出来删除停用表.但是从我读过的内容来看,最好保留数据库表信息,直到管理员卸载而不是停用.
我一直在寻找答案,但我似乎能找到的只是停用它们.我也有我需要用它卸载的版本选项.
mad*_*opt 13
你不应该像bhuthecoder说的那样制作uninstall.php.您可以按照自己的意愿命名文件,也可以在单个文件中命名,而无需单独的文件进行卸载.
register_uninstall_hook('uninstall.php', 'on_uninstall');
Run Code Online (Sandbox Code Playgroud)
这意味着,WP将运行uninstall.php和功能on_uninstall在uninstall.php时,该插件将被删除.因此,您可以根据需要重命名文件,也可以重命名该功能.
register_uninstall_hook(__FILE__, 'on_uninstall');
Run Code Online (Sandbox Code Playgroud)
它的含义相同,但__FILE__指示当前正在运行的当前文件,函数on_uninstall应该在此文件中.
__FILE__ php.net
已解决符号链接的文件的完整路径和文件名.如果在include中使用,则返回包含文件的名称.
激活/取消激活/卸载功能的简单示例.
function on_activation()
{
//Some stuff
}
function on_deactivation()
{
//Some stuff
}
function on_uninstall()
{
//Some stuff
}
register_activation_hook(__FILE__, 'on_activation');
register_deactivation_hook(__FILE__, 'on_deactivation');
register_uninstall_hook(__FILE__, 'on_uninstall');
Run Code Online (Sandbox Code Playgroud)
如果您添加了一些选项,则可以在卸载时删除它,如下所示:
delete_option( 'some name' );
Run Code Online (Sandbox Code Playgroud)
正如bhuthecoder所说,你不应该在停用时丢弃表,这对于用户来说是多余的并且不友好,用户在停用后可能会丢失数据.
您的代码中存在语法错误:
$sql = 'DROP TABLE IF EXISTS $table_name;';
Run Code Online (Sandbox Code Playgroud)
应该是这样的:
$sql = "DROP TABLE IF EXISTS $table_name";
Run Code Online (Sandbox Code Playgroud)
如果你想在字符串中加入一些变量,你应该使用双引号.
而且require_once是多余的.更像这样:
function e34s_db_clients_uninstall()
{
global $wpdb;
$table_name = $wpdb->prefix . 'e34s_clients';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
delete_option('e34s_time_card_version');
}
register_uninstall_hook(__FILE__, 'e34s_db_clients_uninstall');
Run Code Online (Sandbox Code Playgroud)
或者像这样:
register_uninstall_hook('uninstall.php', 'e34s_db_clients_uninstall');
Run Code Online (Sandbox Code Playgroud)
随着功能e34s_db_clients_uninstall在uninstall.php.
有两种方法可以对插件卸载/删除进行此操作。
我学到的最好的方法是使用uninstall.php
uninstall.php 仅在从插件中删除或删除插件时加载,您可以将任何代码放入其中以在用户删除或卸载插件时运行。
<?php
/*
* Removing Plugin data using uninstall.php
* the below function clears the database table on uninstall
* only loads this file when uninstalling a plugin.
*/
/*
* exit uninstall if not called by WP
*/
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit();
}
/*
* Making WPDB as global
* to access database information.
*/
global $wpdb;
/*
* @var $table_name
* name of table to be dropped
* prefixed with $wpdb->prefix from the database
*/
$table_name = $wpdb->prefix . 'table_name_to_be_dropped';
// drop the table from the database.
$wpdb->query( "DROP TABLE IF EXISTS $table_name" );
Run Code Online (Sandbox Code Playgroud)
通过您的表名更改“table_name_to_be_dropped”。
好吧,当我用谷歌搜索这个问题时,我得到了很多结果。这是我得到的:-
function mr_np_activate(){
// hook uninstall
if ( function_exists('register_uninstall_hook') )
register_uninstall_hook(__FILE__,'mr_np_uninstall');
}
register_activation_hook(__FILE__,'mr_np_activate');
function mr_np_uninstall() {
delete_option('my_plugins_options');
}
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息:http://codex.wordpress.org/Function_Reference/register_uninstall_hook
| 归档时间: |
|
| 查看次数: |
11133 次 |
| 最近记录: |