WordPress插件開(kāi)發(fā)筆記(二):啟用/禁用/卸載插件

接下來(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í)行, 原因不詳.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本地路徑 把 PHP 常量 FILE 傳遞給了 plugin_dir_path() 函數(shù)。這會(huì)生成指向你的插件目錄...
    StevenQin閱讀 459評(píng)論 0 0
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評(píng)論 19 139
  • 插件存放目錄 wp-content/plugins 創(chuàng)建一個(gè)插件 在plugins創(chuàng)建一個(gè)文件插件文件夾,命名最好...
    IT錕閱讀 594評(píng)論 0 4
  • 這是我開(kāi)發(fā)插件LaTeX2HTML的學(xué)習(xí)筆記. 首先, 一個(gè)標(biāo)準(zhǔn)的插件, 至少需要以下 唯一的插件名字: late...
    破舊的大卡車(chē)閱讀 948評(píng)論 0 0
  • 動(dòng)作鉤子 ( actions ) 動(dòng)作鉤子讓你可以在 WordPress 加載過(guò)程中或者當(dāng)某個(gè)事件發(fā)生的特定時(shí)刻觸...
    StevenQin閱讀 573評(píng)論 0 0

友情鏈接更多精彩內(nèi)容