接下來(lái), 我們用一個(gè)類(lèi)模板來(lái)啟用/禁用/卸載插件. 該類(lèi)文件來(lái)自WPSE.
在主文件latex2html.php末尾添加內(nèi)容如下:
<?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
}
在下一次, 我們將涉及如何設(shè)置插件的自定義頁(yè)面.
附注: 可以參考php類(lèi)文檔以及WP的register_activation_hook中會(huì)提及的一些注意事項(xiàng), 例如全局變量的引用.
疑問(wèn): 我嘗試將上述代碼放入到一個(gè)文件includes/active-deactive.php, 然后在主文件中include_once調(diào)入, 發(fā)現(xiàn)該文件沒(méi)有執(zhí)行, 原因不詳.