0

I made my first Wordpress plugin and I would get that crease a datatable in worpress databases. I tried the next code buy I get nothing:

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);
}

This functions will be do something when we enable/disabled the plugin...buy isn't works.

And other question...how I made the same event when I install the plugin?

Thanks!

2 Answers 2

0

I found the solution:

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);*/
}

I wrote wrong the create sentence and ussing $wpdb->query($create) works!

Thanks for all!

0

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

should probably read:

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

You really should use the dbDelta() function as WordPress will assist with future Table upgrades/edits. Otherwise you will have to manually handle the database upgrades. Not fun.

Your final code will read something like this:

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);
}

Also when you uninstall the plugin you really should put all that into an uninstall.php file saved in your plugin directory. This is the proper way to uninstall a plugin: 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" );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.