1

I'm developing a wordpress plugin but I have a problem: when I active the plugin, I get the error message "The plugin generated 3 characters of unexpected output"

My purpose is create a plugin that creates some tables when is activate and destroy them on unactivate.

This is my main file code:

<?php
/*
Plugin Name: _Innovation Factory
Description: Aportaci&oacute;n y mantenimiento de ideas del staff para mejorar el hotel.    
Version: 0.1    
Author: Extra Software - Thankium
*/

require_once('includes/php/constants.php');

//creacion de las tablas necesarios
function innovationFactoryActivacion() {
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_FASES_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `nombre` VARCHAR(45) NOT NULL,
        PRIMARY KEY (`id`),
        UNIQUE INDEX `nombre_UNIQUE` (`nombre` ASC));"; 

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_ESTADOS_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `nombre` VARCHAR(45) NOT NULL,
        PRIMARY KEY (`id`),
        UNIQUE INDEX `nombre_UNIQUE` (`nombre` ASC));";

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_HOTELES_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `nombre` VARCHAR(45) NOT NULL,
        PRIMARY KEY (`id`),
        UNIQUE INDEX `nombre_UNIQUE` (`nombre` ASC));";

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_AREAS_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `nombre` VARCHAR(45) NOT NULL,
        PRIMARY KEY (`id`),
        UNIQUE INDEX `nombre_UNIQUE` (`nombre` ASC));";

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_IDEAS_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `anonima` TINYINT NOT NULL,
        `fecha_alta` TIMESTAMP NULL DEFAULT NOW(),
        `nombre` VARCHAR(250) NULL,
        `observacion` TEXT NULL,
        `desarrollo` TEXT NOT NULL,
        `id_hotel` BIGINT(20) NULL,
        `id_estado` BIGINT(20) NOT NULL,
        `id_fase` BIGINT(20) NOT NULL,
        `id_area` BIGINT(20) NOT NULL,
        `id_usuario` BIGINT(20) NOT NULL,
        PRIMARY KEY (`id`));";

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_ADJUNTOS_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `nombre` VARCHAR(1000) NOT NULL,
        `fecha_alta` TIMESTAMP NOT NULL DEFAULT now(),
        `id_idea` BIGINT(20) UNSIGNED NOT NULL,
        PRIMARY KEY (`id`),
        INDEX `ID_IDEA_idx` (`id_idea` ASC),
        CONSTRAINT `ID_IDEA`
        FOREIGN KEY (`id_idea`)
        REFERENCES `" . _PREFIJO_PLUGIN_ . _TABLA_IDEAS_ . "` (`id`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION);";

    $sql[] = "CREATE TABLE IF NOT EXISTS " . _PREFIJO_PLUGIN_ . _TABLA_VOTACIONES_ . " (
        `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        `id_usuario` BIGINT(20) UNSIGNED NOT NULL,
        `id_idea` BIGINT(20) UNSIGNED NOT NULL,
        `valoracion` INT NOT NULL,
        `fecha_valoracion` TIMESTAMP NOT NULL DEFAULT NOW(),
        PRIMARY KEY (`id`),
        INDEX `ID_IDEA_idx` (`id_idea` ASC),
        INDEX `ID_USUARIO_idx` (`id_usuario` ASC),
        CONSTRAINT `ID_IDEA`
        FOREIGN KEY (`id_idea`)
        REFERENCES `" . _PREFIJO_PLUGIN_ . _TABLA_IDEAS_ . "` (`id`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
        CONSTRAINT `ID_USUARIO`
        FOREIGN KEY (`id_usuario`)
        REFERENCES `".$wpdb->prefix."users` (`ID`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION);";

    $sql[] = "INSERT INTO " . _PREFIJO_PLUGIN_ . _TABLA_FASES_ . "(`nombre`) VALUES ('Buzón'), ('Análisis'), ('Piloto'), ('Standard');";
    $sql[] = "INSERT INTO " . _PREFIJO_PLUGIN_ . _TABLA_ESTADOS_ . "(`nombre`) VALUES ('Recibida'), ('Aprobada'), ('Rechazada'), ('Publicada'), ('En proceso'), ('Finalizada');";
    $sql[] = "INSERT INTO " . _PREFIJO_PLUGIN_ . _TABLA_AREAS_ . "(`nombre`) VALUES ('Producto'), ('Servicio'), ('Cliente');";
    $sql[] = "INSERT INTO " . _PREFIJO_PLUGIN_ . _TABLA_HOTELES_ . "(`nombre`) VALUES ('Petit Palace Alcalá'), ('Icon Retiro by Petit Palace'), ('Petit Palace Arana'), ('Petit Palace Ópera'), ('Petit Palace Art Gallery'), ('Petit Palace Arturo Soria'), ('Petit Palace Barcelona'), ('Old: Boquería y Opera Garden'), ('Petit Palace Plaza de la Reina'), ('Petit Palace Sevilla'), ('Petit Palace Callao'), ('Petit Palace Chueca'), ('Petit Palace Serrano'), ('Petit Palace Ruzafa'), ('Petit Palace El Prado'), ('Petit Palace Preciados'), ('Petit Palace Madrid Aeropuerto'), ('Petit Palace Catedral'), ('Petit Palace Plaza Mayor'), ('Petit Palace Paseo de Gracia'), ('Petit Palace Plaza Larios'), ('Petit Palace Plaza del Carmen'), ('Petit Palace Posada del Peine'), ('Petit Palace Castellana'), ('Petit Palace Puerta del Sol'), ('Petit Palace Plaza España'), ('Petit Palace Santa Bárbara Plaza'), ('Petit Palace Santa Cruz'), ('Petit Palace Tamarises'), ('Petit Palace Gran Vía'), ('Petit Palace Triball');";

    foreach($sql as $query){
        dbDelta($query);
    }

}
// run the install scripts upon plugin activation
register_activation_hook(__FILE__, 'innovationFactoryActivacion');

register_deactivation_hook(__FILE__, function(){
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    $sql = array();
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_VOTACIONES_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_ADJUNTOS_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_IDEAS_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_AREAS_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_HOTELES_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_ESTADOS_.";";
    $sql[] = "DROP TABLE IF EXISTS "._PREFIJO_PLUGIN_._TABLA_FASES_.";";

    foreach($sql as $query){
        $wpdb->query($query);
    }
});

The tables are correctly created and dropped, but I don`t kown whay I have this output.

Maybe it has relation with my FK constraints in some tables?

Thanks you...

msolla
  • 306
  • 1
  • 3
  • 16

2 Answers2

1

Your WordPress database tables have Foreign Key Constrains. It means one of the field in one table is referenced as one of the field of other table. So basically, when you are dropping one table, if you have this foreign key constrain, those fields may not dropped.

Below you can read some detailed information.

https://gauravsohoni.wordpress.com/2009/03/09/mysql-disable-foreign-key-checks-or-constraints/

Can a foreign key be NULL and/or duplicate?

Community
  • 1
  • 1
Milap
  • 6,915
  • 8
  • 26
  • 46
  • But the problem is when activated... in adiction, now I had seen that wordpress tables are created in MyIssam engine, wich have no supports for foreign keys. – msolla Jul 15 '16 at 09:49
0

$wpdb is creating the problem. in your function declare global $wpdb;

Sherif
  • 11,786
  • 3
  • 32
  • 57
Mickey
  • 534
  • 2
  • 9
  • it is declared on constants.php file. Nedd I redeclare in each function? – msolla Jul 20 '16 at 08:07
  • before you get access any to global variable in PHP inside a function must be decleared as global, otherwise it will be treated like a local variable – Mickey Jul 20 '16 at 11:05
  • oh, thanks! but on activate it stills printing "The plugin generated 3 characters of unexpected output" – msolla Jul 21 '16 at 07:18