如何制作自动安装插件?

时间:2014-09-25 作者:Marcos

我制作了我的第一个Wordpress插件,我将在Wordpress数据库中获得一个数据表。我尝试了下一个代码购买,但一无所获:

register_activation_hook(__FILE__, \'rating_install\');
register_deactivation_hook(__FILE__, \'rating_uninstall\');
function rating_install(){
//Funcion que genera la instalacion del plugin
global $wpdb;
$table_name = $wpdb->prefix."rating";
$create = "CRETA TABLE ".$table_name." "
        . "(id int(11) NOT NULL AUTO_INCREMENT, "
        . "url tinytext NOT NULL, "
        . "descripcion tinytext NOT NULL)"
        . "PRIMARY KEY (\'id\')";
require_once(ABSPATH."wp-admin/includes/upgrade.php");
dbDelta($create);
}

function rating_uninstall(){
    //Funcion que genera la desinstalacion del plugin
    global $wpdb;
    $table_name = $wpbd->prefix."rating";
    $drop = "DROP TABLE IF EXISTS ".$table_name;
    require_once(ABSPATH."wp-admin/includes/upgrade.php");
    dbDelta($drop);
}
当我们启用/禁用插件时,这些函数将起作用。。。买不起作用。

还有其他问题。。。当我安装插件时,我是如何做出相同的事件的?

谢谢

2 个回复
SO网友:Marcos

我找到了解决方案:

function rating_install(){
    //Funcion que genera la instalacion del plugin
    global $wpdb;
    $table_name = $wpdb->prefix."rating";
    $create = "CRETA TABLE ".$table_name." ( "
            . "id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY, "
            . "url TINYTEXT NOT NULL, "
            . "descripcion TINYTEXT NOT NULL ) ";    
    $wpdb->query($create);
    /*require_once(ABSPATH."wp-admin/includes/upgrade.php");
    dbDelta($create);*/
}

function rating_uninstall(){
    //Funcion que genera la desinstalacion del plugin
    global $wpdb;
    $table_name = $wpdb->prefix."rating";
    $drop = "DROP TABLE IF EXISTS ".$table_name;
    $wpdb->query($drop);
    /*require_once(ABSPATH."wp-admin/includes/upgrade.php");
    dbDelta($drop);*/
}
我写错了create语句,使用$wpdb->查询($create)很有效!

谢谢大家!

SO网友:jharrell

$create = "CRETA TABLE ".$table_name." ( "

可能应阅读:

$create = "CREATE TABLE ".$table_name." ( "

您确实应该使用dbDelta()函数,因为WordPress将有助于将来的表升级/编辑。否则,您将不得不手动处理数据库升级。不好玩。

您的最终代码将如下所示:

register_activation_hook(__FILE__, \'rating_install\');

function rating_install(){
//Funcion que genera la instalacion del plugin
global $wpdb;
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
$table_name = $wpdb->prefix .\'rating\';    
$create = 
      "CREATE TABLE ".$table_name." (
      `id` INT NOT NULL AUTO_INCREMENT,
      `url` TINYTEXT NOT NULL,
      `descripcion` TINYTEXT NOT NULL,
      PRIMARY KEY (`id`));";
dbDelta($create);
}
此外,当你卸载插件时,你真的应该把所有这些都放到卸载中。php文件保存在插件目录中。这是卸载插件的正确方法:http://codex.wordpress.org/Function_Reference/register_uninstall_hook

<?php
// uninstall.php

//if uninstall not called from WordPress exit
if ( !defined( \'WP_UNINSTALL_PLUGIN\' ) ) 
    exit();

$option_name = \'any added option name\';
delete_option( $option_name );

// For site options in multisite
delete_site_option( $option_name );

//drop a custom db table
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS {$wpbd->prefix}rating" );

结束

相关推荐

Custom WordPress Installation

我想知道下面的帖子中描述的自定义WordPress安装的方法是否有更新版本。http://wpbits.wordpress.com/2007/08/10/automating-wordpress-customizations-the-installphp-way/