I am creating a plugin for a custom form in wordpress, but when I activate it, it does not create the table in the database, to say that I am new to programming in wordpress and I am very green.
function InscripSolabria_init()
{
global $wpdb; // Este objeto global nos permite trabajar con la BD de WP
// Crea la tabla si no existe
$tabla_inscritos = $wpdb->prefix . 'inscritos';
$tabla_actividades = $wpdb->prefix . 'actividades';
$charset_collate = $wpdb->get_charset_collate();
$query = "CREATE TABLE IF NOT EXISTS $tabla_inscritos (
id_inscritos INT(9) NOT NULL AUTO_INCREMENT PRIMARY KEY,
nombre varchar(40) NOT NULL,
correo varchar(100) NOT NULL,
telefono varchar(40) NOT NULL,
FK_id_actividades INT(4) NOT NULL FOREIGN KEY
)
$charset_collate;";
$query2 = "CREATE TABLE IF NOT EXISTS $tabla_actividades(
id_actividades INT(9) NOT NULL AUTO_INCREMENT PRIMARY KEY,
actividades varchar(100) NOT NULL,
imagen varchar(100) NULL,
horarios varchar(500)NULL
REFERENCES(id_actividades) FOREIGN KEY inscritos(FK_id_actividades)
)$charset_collate;";
// La función dbDelta que nos permite crear tablas de manera segura se
// define en el fichero upgrade.php que se incluye a continuación
include_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($query,$query2);
}
From what you expose and the way the function works, you must be creating the table
inscritos
, but not the one fromactividades
, because you are not supplying the parameters correctly for dbDelta() :;
or array with one or more queries as stringSo, if the function internally works with arrays, I think the most appropriate thing is to send your queries inside an array, enclosing it in square brackets:
Additionally, I think you have an error creating the second table:
I recommend you first test the queries in PhpMyAdmin to see if they work or apply any necessary fixes. Then you integrate them into the plugin .
The error is based on the fact that you are not using the backsticks to identify tables or columns. The proposed solution is the following