"Headers already sent" error when creating tables in plugin activation

Asked

Viewed 829 times

0

I am developing a plugin for Wordpress, in which during the activation, two tables that relate should be created. My code is like this:

function create_tables(){
    global $wpdb;
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    // Query para a criação da tabela de parceiros.
    $sql_partners = "CREATE TABLE ".$wpdb->prefix."partners(
                    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                    name VARCHAR(355) NOT NULL,
                    phone VARCHAR(12),
                    address VARCHAR(355) NOT NULL
                    );";

    dbDelta($sql_partners);

    // Query para a criação da tabela de categorias. Parceiros são agrupados por categoria.
    $sql_categories = "CREATE TABLE ".$wpdb->prefix."partner_categories(
                      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                      name VARCHAR(100) NOT NULL,
                      FOREIGN_KEY (partner_id) references partners(id)
                      );";

    dbDelta($sql_categories);
}

This function is already properly configured in the register_activation_hook plugin. The first table is created. But the second, it seems, is simply ignored and I have the following error on the activation screen:

This plugin generated 875 unexpected output characters during activation. If you encounter messages from "headers already sent" issues with feeds or others, try disabling or removing the plugin.

I have tried several things, among them change the encoding of the file. Just nothing works. Somebody’s been there and they could tell me what’s wrong there?

  • 1

    I don’t know much SQL so I don’t know the solution, but the problem is your second query. I don’t know where the partner_id and the REFERENCES should point to the other table with prefix (is not in the query). PHP error log shows errors.

  • 1

    In addition to the error pointing by @brasofilo, apparently the function dbDelta() may not be able to parse your query. According to Codex from what I could see: 1) The definition of PRIMARY KEY must be "delayed", that is, made after the definition of the fields. 2) Field types should be written in lower case letters.

  • 1

    The paper you highlighted in yellow is already part of the answer to the question. Your plugin generated an output message, which is probably composed of errors and warnings. Looking at the log, as mentioned by @brasofilo, you will probably have the explanation, or at least a notion of what is happening.

  • That is if enabled

1 answer

2


Besides you put it FOREIGN_KEY (partner_id) and should be the id of the same table, that is to say, FOREIGN_KEY (id). And as colleagues have said, you forgot the prefix when doing the FOREIGN KEY. Try this way:

function create_tables(){
    global $wpdb;
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    // Query para a criação da tabela de parceiros.
    $sql_partners = "CREATE TABLE ".$wpdb->prefix."partners(
                    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                    name VARCHAR(355) NOT NULL,
                    phone VARCHAR(12),
                    address VARCHAR(355) NOT NULL
                    );";

    dbDelta($sql_partners);

    // Query para a criação da tabela de categorias. Parceiros são agrupados por categoria.
    $sql_categories = "CREATE TABLE ".$wpdb->prefix."partner_categories(
                      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                      name VARCHAR(100) NOT NULL,
                      FOREIGN_KEY (id) references ".$wpdb->prefix."partners(id)
                      );";

    dbDelta($sql_categories);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.