您将看不到任何视觉输出<但是我添加了exit()
对所有不同回调的声明,以便您能够了解实际发生的情况。只需取消对它们的注释,就可以看到它们在工作检查一下__FILE__ != WP_PLUGIN_INSTALL
和(如果不是:中止!)看看是否真的在卸载插件。我建议直接触发on_deactivation()
在开发过程中进行回调,这样您就节省了恢复所有内容所需的时间。至少我是这样做的我还得做些保安工作。有些是由core完成的,但是嘿<安全总比抱歉好<首先,我不允许在未加载core时直接访问文件:defined( \'ABSPATH\' ) OR exit;
然后我检查是否允许当前用户执行此任务最后一项任务是检查推荐人。注意:使用wp_die()
当您遇到错误时,屏幕会询问适当的权限(如果您想再试一次…,当然可以)。当core重定向您、设置当前$GLOBALS[\'wp_list_table\']->current_action();
到error_scrape
然后检查推荐人check_admin_referer(\'plugin-activation-error_\' . $plugin);
, 哪里$plugin
是$_REQUEST[\'plugin\']
. 因此,重定向发生在页面加载量的一半时,您会看到这个有线滚动条,并看到黄色的管理通知/消息框。如果发生这种情况:保持冷静,用一些exit()
以及逐步调试(A)普通函数插件请记住,如果在函数定义之前挂接回调,则这可能不起作用。<?php
defined( \'ABSPATH\' ) OR exit;
/**
* Plugin Name: (WCM) Activate/Deactivate/Uninstall - Functions
* Description: Example Plugin to show activation/deactivation/uninstall callbacks for plain functions.
* Author: Franz Josef Kaiser/wecodemore
* Author URL: http://unserkaiser.com
* Plugin URL: http://wordpress.stackexchange.com/questions/25910/uninstall-activate-deactivate-a-plugin-typical-features-how-to/25979#25979
*/
function WCM_Setup_Demo_on_activation()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
$plugin = isset( $_REQUEST[\'plugin\'] ) ? $_REQUEST[\'plugin\'] : \'\';
check_admin_referer( "activate-plugin_{$plugin}" );
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
function WCM_Setup_Demo_on_deactivation()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
$plugin = isset( $_REQUEST[\'plugin\'] ) ? $_REQUEST[\'plugin\'] : \'\';
check_admin_referer( "deactivate-plugin_{$plugin}" );
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
function WCM_Setup_Demo_on_uninstall()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
check_admin_referer( \'bulk-plugins\' );
// Important: Check if the file is the one
// that was registered during the uninstall hook.
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
register_activation_hook( __FILE__, \'WCM_Setup_Demo_on_activation\' );
register_deactivation_hook( __FILE__, \'WCM_Setup_Demo_on_deactivation\' );
register_uninstall_hook( __FILE__, \'WCM_Setup_Demo_on_uninstall\' );
(B)基于类的/面向对象架构这是当今插件中最常见的例子。<?php
defined( \'ABSPATH\' ) OR exit;
/**
* Plugin Name: (WCM) Activate/Deactivate/Uninstall - CLASS
* Description: Example Plugin to show activation/deactivation/uninstall callbacks for classes/objects.
* Author: Franz Josef Kaiser/wecodemore
* Author URL: http://unserkaiser.com
* Plugin URL: http://wordpress.stackexchange.com/questions/25910/uninstall-activate-deactivate-a-plugin-typical-features-how-to/25979#25979
*/
register_activation_hook( __FILE__, array( \'WCM_Setup_Demo_Class\', \'on_activation\' ) );
register_deactivation_hook( __FILE__, array( \'WCM_Setup_Demo_Class\', \'on_deactivation\' ) );
register_uninstall_hook( __FILE__, array( \'WCM_Setup_Demo_Class\', \'on_uninstall\' ) );
add_action( \'plugins_loaded\', array( \'WCM_Setup_Demo_Class\', \'init\' ) );
class WCM_Setup_Demo_Class
{
protected static $instance;
public static function init()
{
is_null( self::$instance ) AND self::$instance = new self;
return self::$instance;
}
public static function on_activation()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
$plugin = isset( $_REQUEST[\'plugin\'] ) ? $_REQUEST[\'plugin\'] : \'\';
check_admin_referer( "activate-plugin_{$plugin}" );
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
public static function on_deactivation()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
$plugin = isset( $_REQUEST[\'plugin\'] ) ? $_REQUEST[\'plugin\'] : \'\';
check_admin_referer( "deactivate-plugin_{$plugin}" );
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
public static function on_uninstall()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
check_admin_referer( \'bulk-plugins\' );
// Important: Check if the file is the one
// that was registered during the uninstall hook.
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
public function __construct()
{
# INIT the plugin: Hook your callbacks
}
}
(C)带有外部安装对象的基于类的/面向对象体系结构此场景假定您有一个主插件文件和另一个名为setup.php
在名为inc
: ~/wp-content/plugins/your_plugin/inc/setup.php
. 当插件文件夹位于默认WP文件夹结构之外时,以及当内容目录重命名或安装文件命名不同时,这也会起作用。只有inc
文件夹必须具有相同的名称(&;相对于插件根目录的位置。注意:您只需取三个register_*_hook()*
函数和类,并将它们放入插件中
主插件文件:
<?php
defined( \'ABSPATH\' ) OR exit;
/**
* Plugin Name: (WCM) Activate/Deactivate/Uninstall - FILE/CLASS
* Description: Example Plugin
* Author: Franz Josef Kaiser/wecodemore
* Author URL: http://unserkaiser.com
* Plugin URL: http://wordpress.stackexchange.com/questions/25910/uninstall-activate-deactivate-a-plugin-typical-features-how-to/25979#25979
*/
register_activation_hook( __FILE__, array( \'WCM_Setup_Demo_File_Inc\', \'on_activation\' ) );
register_deactivation_hook( __FILE__, array( \'WCM_Setup_Demo_File_Inc\', \'on_deactivation\' ) );
register_uninstall_hook( __FILE__, array( \'WCM_Setup_Demo_File_Inc\', \'on_uninstall\' ) );
add_action( \'plugins_loaded\', array( \'WCM_Setup_Demo_File\', \'init\' ) );
class WCM_Setup_Demo_File
{
protected static $instance;
public static function init()
{
is_null( self::$instance ) AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
add_action( current_filter(), array( $this, \'load_files\' ), 30 );
}
public function load_files()
{
foreach ( glob( plugin_dir_path( __FILE__ ).\'inc/*.php\' ) as $file )
include_once $file;
}
}
安装文件:<?php
defined( \'ABSPATH\' ) OR exit;
class WCM_Setup_Demo_File_Inc
{
public static function on_activation()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
$plugin = isset( $_REQUEST[\'plugin\'] ) ? $_REQUEST[\'plugin\'] : \'\';
check_admin_referer( "activate-plugin_{$plugin}" );
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
public static function on_deactivation()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
$plugin = isset( $_REQUEST[\'plugin\'] ) ? $_REQUEST[\'plugin\'] : \'\';
check_admin_referer( "deactivate-plugin_{$plugin}" );
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
public static function on_uninstall()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
check_admin_referer( \'bulk-plugins\' );
// Important: Check if the file is the one
// that was registered during the uninstall hook.
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
# Uncomment the following line to see the function in action
# exit( var_dump( $_GET ) );
}
}
(2)插件更新如果您编写的插件有自己的DB表或选项,可能会出现需要更改或升级的情况。遗憾的是,到目前为止,还不可能在插件/主题安装或更新/升级上运行某些东西。很高兴有一个解决方法:将自定义函数挂接到自定义选项上(是的,这很蹩脚,但很有效)。
function prefix_upgrade_plugin()
{
$v = \'plugin_db_version\';
$update_option = null;
// Upgrade to version 2
if ( 2 !== get_option( $v ) )
{
if ( 2 < get_option( $v ) )
{
// Callback function must return true on success
$update_option = custom_upgrade_cb_fn_v3();
// Only update option if it was an success
if ( $update_option )
update_option( $v, 2 );
}
}
// Upgrade to version 3, runs just after upgrade to version 2
if ( 3 !== get_option( $v ) )
{
// re-run from beginning if previous update failed
if ( 2 < get_option( $v ) )
return prefix_upgrade_plugin();
if ( 3 < get_option( $v ) )
{
// Callback function must return true on success
$update_option = custom_upgrade_cb_fn_v3();
// Only update option if it was an success
if ( $update_option )
update_option( $v, 3 );
}
}
// Return the result from the update cb fn, so we can test for success/fail/error
if ( $update_option )
return $update_option;
return false;
}
add_action(\'admin_init\', \'prefix_upgrade_plugin\' );
<支持>Source这个更新函数不是一个很好/写得不好的示例,但正如所说的那样:这是一个示例,该技术运行良好。将在以后的更新中对此进行改进。