Plugin generated invalid XXX characters during activation

Asked

Viewed 277 times

0

I am studying creating plugins for Wordpress and as an initial part of my studies is to create a plugin administration of school newsletters of students of a fictitious school.

I need to create some tables in the database as soon as the user activates the plugin. According to the wordpress documentation, one of the ways to do this would be to create SQL queries inside the file main.php in the plugin’s initial directory and then register the functions with register_activation_hook(__FILE__, 'function');.

However, when saving the main file of my plugin and activating it in the administrative panel of Wordpress, the following warning message is generated:

The plugin generated 808 unexpected output characters during activation. If you notice messages from "headers already sent", problems with feeds or other issues, try to disable or remove this plugin.

When checking the database, I notice that neither the table nor the records were entered. I cannot understand what these additional characters would be since I would not have to debug my code to identify the error.

Below is my code. I thank you for all your help!

<?php

/**
 * Plugin Name: Boletim de Notas
 * Plugin URL: http://www.edinaldoribeiro.com.br
 * Description: Plugin de administração de notas e geração de boletins escolares online.
 * Version: 1.0
 * Requires at least: 5.2
 * Requires PHP: 7.2
 * Author: Edinaldo Ribeiro
 * Author URI: http://www.edinaldoribeiro.com.br
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

defined('ABSPATH') or die('Página indisponível');

global $db_version;
$db_version = '1.0';

function install_plugin(){

    global $wpdb;
    global $db_version;

    $table = $wpdb->prefix . "estudantes";
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table (
        id INT NOT NULL AUTO_INCREMENT,
        time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
        name tinytext NOT NULL DEFAULT 'unnamed',
        age INT NOT NULL DEFAULT 0,
        url varchar(55) NOT NULL DEFAULT '',
        PRIMARY KEY  (id)
    ) $charset_collate;";

    require_once(ABSPATH.'wp-admin/includes/upgrade.php');
    dbDelta($sql);

    add_option('db_version', $db_version);
}

function install_plugin_data() {
    global $wpdb;

    $name = "Edinaldo Ribeiro";
    $age = '23';

    $table = $wpdb->prefix . 'estudantes';

    $wpdb->insert(
        $table,
        array(
            'time' => current_time('mysql'),
            'name' => $name,
            'age' => $age
        )
    );
}

register_activation_hook(__FILE__, 'install_plugin');
register_activation_hook(__FILE__, 'install_plugin_data');

1 answer

1

I managed to solve the problem the next day with a fresh head, rsss.. I’ll leave the solution, maybe I can help someone in the future.

The error was in SQL Statement within the function install_plugin() and the invalid characters were only the NOTICES and WARNING that PHP was returning when trying to execute the wrong query.

Follows how SQL was. The rest of the code has not changed.

$sql = "CREATE TABLE $table (
        id INT NOT NULL AUTO_INCREMENT,
        time DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL,
        name tinytext NOT NULL,
        age INT NOT NULL,
        url varchar(55) DEFAULT '' NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

Browser other questions tagged

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