我正在制作一个wordpress插件。我应该在卸载功能中包括哪些典型的东西?
例如,是否应该删除在安装功能中创建的任何表?
我要清理我的期权条目吗?
还要别的吗?
我正在制作一个wordpress插件。我应该在卸载功能中包括哪些典型的东西?
例如,是否应该删除在安装功能中创建的任何表?
我要清理我的期权条目吗?
还要别的吗?
Answers:
下面显示了正确挂钩在上述操作期间触发的回调函数的正确方法。
因为您可以在使用以下代码的插件中使用此代码
我将展示三个不同的演示插件,您可以检查它们,然后在自己的插件中实施代码。
由于该主题非常困难且非常详细,并且有十几个极端案例,因此该答案永远不会是完美的。我会随着时间的推移不断改进它,因此请定期检查。
插件设置回调由core触发,您对core的执行方式没有影响。有一些事情要记住:
echo/print
在安装回调过程中,从不,任何(!)。这将导致出现headers already sent
消息,核心将建议停用并删除您的插件...不要问:我知道...exit()
在所有不同的回调中添加了语句,以便您可以了解实际发生的情况。只需取消注释即可看到工作正常。__FILE__ != WP_PLUGIN_INSTALL
和(如果不是:中止!)以查看是否确实在卸载插件,这一点非常重要。我建议仅on_deactivation()
在开发过程中触发回调,这样可以节省您将所有内容恢复原状的时间。至少这是我要做的。defined( 'ABSPATH' ) OR exit;
wp_die()
要求您提供适当权限的屏幕(如果要再次尝试... 是的,请确保)。这种情况为核心您重定向,设置当前$GLOBALS['wp_list_table']->current_action();
要error_scrape
,然后检查引荐的check_admin_referer('plugin-activation-error_' . $plugin);
,这里$plugin
是$_REQUEST['plugin']
。因此,重定向发生在页面加载量的一半时,您将获得此有线滚动条,并且die屏幕会看到黄色的管理员通知/消息框。如果发生这种情况:请保持冷静,并通过一些exit()
逐步调试来查找错误。请记住,如果在函数定义之前挂接了回调,这可能不起作用。
<?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' );
这是当今插件中最常见的示例。
<?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
}
}
此方案假定你有一个主要的插件文件并命名第二个文件setup.php
中指定的插件的子目录inc
:~/wp-content/plugins/your_plugin/inc/setup.php
。当plugin文件夹不在默认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 ) );
}
}
如果编写的插件具有自己的数据库表或选项,则在某些情况下可能需要更改或升级。
遗憾的是,到目前为止,无法在插件/主题安装或更新/升级上运行某些东西。很高兴有一个解决方法:将自定义函数挂接到自定义选项(是的,这很la脚-但它可以工作)。
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' );
此更新功能不是一个很好/写得很好的示例,但是如前所述:这是一个示例,该技术效果很好。在以后的更新中将对此进行改进。
check_admin_referer()
。他们不需要进行消毒,因为core本身不会这样做,并且无论如何都会将其与未消毒的$_REQUEST
值进行比较。但是,如果他们开始哭像,因为那个小女孩,只要使用filter_var()
或esc_attr()
在其上。
要测试当前系统的必需特性(如PHP版本或已安装的扩展名),可以使用以下类似的代码:
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Check Plugin Requirements
* Description: Test for PHP version and installed extensions
* Plugin URI:
* Version: 2013.03.31
* Author: Thomas Scholz
* Author URI: http://toscho.de
* Licence: MIT
* License URI: http://opensource.org/licenses/MIT
*/
/*
* Don't start on every page, the plugin page is enough.
*/
if ( ! empty ( $GLOBALS['pagenow'] ) && 'plugins.php' === $GLOBALS['pagenow'] )
add_action( 'admin_notices', 't5_check_admin_notices', 0 );
/**
* Test current system for the features the plugin needs.
*
* @return array Errors or empty array
*/
function t5_check_plugin_requirements()
{
$php_min_version = '5.4';
// see http://www.php.net/manual/en/extensions.alphabetical.php
$extensions = array (
'iconv',
'mbstring',
'id3'
);
$errors = array ();
$php_current_version = phpversion();
if ( version_compare( $php_min_version, $php_current_version, '>' ) )
$errors[] = "Your server is running PHP version $php_current_version but
this plugin requires at least PHP $php_min_version. Please run an upgrade.";
foreach ( $extensions as $extension )
if ( ! extension_loaded( $extension ) )
$errors[] = "Please install the extension $extension to run this plugin.";
return $errors;
}
/**
* Call t5_check_plugin_requirements() and deactivate this plugin if there are error.
*
* @wp-hook admin_notices
* @return void
*/
function t5_check_admin_notices()
{
$errors = t5_check_plugin_requirements();
if ( empty ( $errors ) )
return;
// Suppress "Plugin activated" notice.
unset( $_GET['activate'] );
// this plugin's name
$name = get_file_data( __FILE__, array ( 'Plugin Name' ), 'plugin' );
printf(
'<div class="error"><p>%1$s</p>
<p><i>%2$s</i> has been deactivated.</p></div>',
join( '</p><p>', $errors ),
$name[0]
);
deactivate_plugins( plugin_basename( __FILE__ ) );
}
用支票检查PHP 5.5:
register_activation_hook
这里-为什么不使用它?还会在此之前或之后触发,register_activation_hook
并且register_activation_hook
即使以上都不通过,也会触发吗?
add_action( 'admin_notices', 't5_check_admin_notices', 0 );
进入激活钩,该插件在不执行检查的情况下激活。。。
admin_notices
。