【问题标题】:How to Drop Tables on Uninstall using WordPress?如何使用 WordPress 在卸载时删除表?
【发布时间】:2014-01-23 12:59:45
【问题描述】:

插件激活时,我在 WordPress 数据库中创建了 4 个表。

卸载时,我希望删除这些表。

我已经能够将其写出来以在停用时删除表。但是从我读到的内容来看,最好保留数据库表信息,直到管理员卸载而不是停用。

我一直在寻找答案,但我似乎能找到的只是将它们停用。我也有需要卸载的版本选项。

【问题讨论】:

    标签: mysql wordpress uninstallation


    【解决方案1】:

    您不应该像 bhuthecoder 所说的那样制作 uninstall.php。您可以根据需要命名文件,也可以在单个文件中执行此操作,而无需单独的文件进行卸载。

    register_uninstall_hook('uninstall.php', 'on_uninstall');
    

    表示当插件被删除时,WP会运行uninstall.phpuninstall.php中的on_uninstall函数。因此,您可以根据需要重命名文件,也可以重命名函数。

    register_uninstall_hook(__FILE__, 'on_uninstall');
    

    意思相同,但__FILE__表示当前正在运行的文件,函数on_uninstall应该在这个文件中。

    __FILE__ php.net

    已解析符号链接的文件的完整路径和文件名。如果使用 在包含中,返回包含文件的名称。

    激活/停用/卸载功能的简单示例。

    function on_activation()
    {
        //Some stuff
    }
    
    function on_deactivation()
    {
        //Some stuff
    }
    
    function on_uninstall()
    {
        //Some stuff
    }
    
    register_activation_hook(__FILE__, 'on_activation');
    register_deactivation_hook(__FILE__, 'on_deactivation');
    register_uninstall_hook(__FILE__, 'on_uninstall');
    

    如果您添加了一些选项,您可以在卸载时将其删除,如下所示:

    delete_option( 'some name' );
    

    正如bhuthecoder所说,你不应该在停用时删除表,这对用户来说是多余的并且不友好,停用后可能会丢失他的数据。

    您的代码中有语法错误:

    $sql = 'DROP TABLE IF EXISTS $table_name;';
    

    应该是这样的:

    $sql = "DROP TABLE IF EXISTS $table_name";
    

    如果你想在字符串中加入一些变量,你应该使用双引号。

    而且 require_once 是多余的。 最好这样:

    function e34s_db_clients_uninstall()
    {
        global $wpdb;
        $table_name = $wpdb->prefix . 'e34s_clients';
        $sql = "DROP TABLE IF EXISTS $table_name";
        $wpdb->query($sql);
        delete_option('e34s_time_card_version');
    }
    
    register_uninstall_hook(__FILE__, 'e34s_db_clients_uninstall');
    

    或者像这样:

    register_uninstall_hook('uninstall.php', 'e34s_db_clients_uninstall');
    

    uninstall.php 中使用函数 e34s_db_clients_uninstall

    【讨论】:

      【解决方案2】:

      有两种方法可以对插件卸载/删除进行此操作。

      我学到的最好方法是使用uninstall.php

      uninstall.php 仅在从插件中删除或删除插件时加载,您可以将任何代码放入其中以在用户删除或卸载插件时运行。

      <?php
      /* 
       * Removing Plugin data using uninstall.php
       * the below function clears the database table on uninstall
       * only loads this file when uninstalling a plugin.
       */
      
      /* 
       * exit uninstall if not called by WP
       */
      if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
          exit();
      }
      
      /* 
       * Making WPDB as global
       * to access database information.
       */
      global $wpdb;
      
      /* 
       * @var $table_name 
       * name of table to be dropped
       * prefixed with $wpdb->prefix from the database
       */
      $table_name = $wpdb->prefix . 'table_name_to_be_dropped';
      
      // drop the table from the database.
      $wpdb->query( "DROP TABLE IF EXISTS $table_name" );
      

      将“table_name_to_be_dropped”更改为您的表名。

      【讨论】:

        【解决方案3】:

        嗯,当我用谷歌搜索这个问题时,我得到了很多结果。这是我得到的:-

        function mr_np_activate(){
            // hook uninstall
            if ( function_exists('register_uninstall_hook') )
                register_uninstall_hook(__FILE__,'mr_np_uninstall');        
        }
        register_activation_hook(__FILE__,'mr_np_activate');    
        
        function mr_np_uninstall() {
            delete_option('my_plugins_options'); 
        }
        

        来源:https://wordpress.stackexchange.com/questions/24600/how-can-i-delete-options-with-register-uninstall-hook

        欲了解更多信息:http://codex.wordpress.org/Function_Reference/register_uninstall_hook

        【讨论】:

          【解决方案4】:

          使用,register_uninstall_hook。 它注册卸载钩子,当用户单击卸载链接时将调用插件,该链接要求插件自行卸载, 详情请查看http://codex.wordpress.org/Function_Reference/register_uninstall_hook

          【讨论】:

          • 我也看到了那个,这是我的代码,但它不起作用:
            在我的主文件 require_once 'uninstall.php'; register_uninstall_hook( __FILE__, 'e34s_db_clients_uninstall' ); 然后在我的卸载.php function e34s_db_clients_uninstall() { global $wpdb; $table_name = $wpdb-&gt;prefix . 'e34s_clients'; $sql = 'DROP TABLE IF EXISTS $table_name;'; $wpdb-&gt;query($sql); delete_option('e34s_time_card_version'); }
          【解决方案5】:

          仅在管理员卸载/删除插件时删除表。

          当管理员停用插件时不要删除表格,因为用户通常会停用所有插件以解决 wordpress 错误修复后他们将重新激活插件,如果您当时删除表格,他们将丢失您配置的所有插件设置by admin 。同时升级 wordpress 时会自动停用所有插件并在升级成功后重新激活。

          卸载时删除表的说明:

          【讨论】:

          • 是的,我已经看到了,这就是为什么我要求在删除时删除创建的表而不是停用的解决方案
          • 添加说明检查一下
          【解决方案6】:

          使用 register_deactivation_hook。这对我有用。

          function on_deactivation() {
          
              global $wpdb;
              $table_name = $wpdb->prefix . 'school';
              $sql        = "DROP TABLE IF EXISTS $table_name";
              $wpdb->query( $sql );
              delete_option( 'wp_install_uninstall_config' );
          }
          
          register_deactivation_hook( __FILE__, 'on_deactivation' );
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-28
            • 2011-05-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-01-27
            相关资源
            最近更新 更多