接下来, 我们用一个类模板来启用/禁用/卸载插件. 该类文件来自WPSE.
在主文件latex2html.php
末尾添加内容如下:
<?php
/**
* The active/deactive/uninstall hooks
*/
register_activation_hook( __FILE__, array( 'active_deactive_class', 'on_activation' ) );
register_deactivation_hook( __FILE__, array( 'active_deactive_class', 'on_deactivation' ) );
register_uninstall_hook( __FILE__, array( 'active_deactive_class', 'on_uninstall' ) );
/**
* Instanize the class
*/
add_action('plugins_loaded', array('active_deactive_class', 'init'));
/**
* The active/deactive class
*/
class active_deactive_class
{
protected static $instance;
public static function init()
{
is_null ( self::$instance ) AND self::$instance = new self;
return self::$instance;
}
/**
* activation
*/
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 ) );
}
/**
* deactivation
*/
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 funcion in action
# exit( var_dump( $_GET ) );
}
/**
* uninstall
*/
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
// thatn 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 ) );
}
/**
* Construct the plugin object
*/
public function __construct()
{
# INIT the plugin: Hook your callbacks
}
在下一次, 我们将涉及如何设置插件的自定义页面.
附注: 可以参考php类文档以及WP的register_activation_hook中会提及的一些注意事项, 例如全局变量的引用.
疑问: 我尝试将上述代码放入到一个文件includes/active-deactive.php
, 然后在主文件中include_once
调入, 发现该文件没有执行, 原因不详.
网友评论