715 posts
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Belgium
  • Has been a member for 5-6 years
  • Bought between 10 and 49 items
Hy-Studio says

Hi,

I’m creating my first WordPress plugin and I’m already hitting a problem. I need to create a table when activating my plugin, but it doesn’t work. here’s my code:

<?php /*
 * Plugin Name: plugin name
 * Description: bla bla bla
 * Version: 1.0
 * Author: Author
 */

register_activation_hook(__FILE__, 'flashplayer_install');

function flashplayer_install(){

    global $wpdb;

    $tbl_name = $wpdb?>prefix.'flashplayer';

    if( $wpdb->get_var("show tables like '$tbl_name'") != $tbl_name ){

        $sql = "CREATE TABLE ".$tbl_name." (
                    id INT(10) NOT NULL AUTO INCREMENT,
                    title TINYTEXT NOT NULL,
                    filename VARCHAR(100) NOT NULL,
                    UNIQUE KEY id(id)
                );";
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);

    }

}

?>

What am I doing wrong here? Anyone knows?

Thanks! :)

753 posts
  • Sold between 10 000 and 50 000 dollars
  • Referred between 10 and 49 users
  • Author had a Free File of the Month
  • Exclusive Author
  • Has been a member for 5-6 years
  • Bought between 1 and 9 items
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
AdamGold says

I suggest you start by “debugging” it so you can see what’s wrong. Instead of creating the table, echo some text.

&lt;?php /*
 * Plugin Name: plugin name
 * Description: bla bla bla
 * Version: 1.0
 * Author: Author
 */

register_activation_hook(__FILE__, 'flashplayer_install');

function flashplayer_install(){

echo 'So far so good.';

}

?&gt;
Now, if all good after that (if you get the text), continue by echoing the query
<?php /*
 * Plugin Name: plugin name
 * Description: bla bla bla
 * Version: 1.0
 * Author: Author
 */

register_activation_hook(__FILE__, 'flashplayer_install');

function flashplayer_install(){

    global $wpdb;

    $tbl_name = $wpdb?>prefix.'flashplayer';

    if( $wpdb->get_var("show tables like '$tbl_name'") != $tbl_name ){

        $sql = "CREATE TABLE ".$tbl_name." (
                    id INT(10) NOT NULL AUTO INCREMENT,
                    title TINYTEXT NOT NULL,
                    filename VARCHAR(100) NOT NULL,
                    UNIQUE KEY id(id)
                );";

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

    }

}

?>
Then paste here the result.
715 posts
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Belgium
  • Has been a member for 5-6 years
  • Bought between 10 and 49 items
Hy-Studio says

Hey Adam,

Well I tried to echo something, but it generates an error when activating:

The plugin generated 12 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

Then when I wanted to echo the query with the var_dump() function I got this when reactivating:

The plugin generated 72 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

Same message as the first one, but with more characters :D

753 posts
  • Sold between 10 000 and 50 000 dollars
  • Referred between 10 and 49 users
  • Author had a Free File of the Month
  • Exclusive Author
  • Has been a member for 5-6 years
  • Bought between 1 and 9 items
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
AdamGold says

Yeah, hmm.. What happens after you activate the plugin? Any problem or something? (Without my code, your original code)

6 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
666Design says

not AUTO INCREMENT but AUTO _INCREMENT , and try to global $wp_roles too

<?php /*
 * Plugin Name: plugin name
 * Description: bla bla bla
 * Version: 1.0
 * Author: Author
 */

require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
global $wpdb , $wp_roles;

register_activation_hook(__FILE__, 'flashplayer_install');

function flashplayer_install(){

    global $wpdb;

    $tbl_name = $wpdb?>prefix.'flashplayer';

    if( $wpdb->get_var("show tables like '$tbl_name'") != $tbl_name ){

        $sql = "CREATE TABLE ".$tbl_name." (
                    id INT(10) NOT NULL AUTO_INCREMENT,
                    title TINYTEXT NOT NULL,
                    filename VARCHAR(100) NOT NULL,
                    UNIQUE KEY id(id)
                );";

        dbDelta($sql);

    }

}

?>

good luck :)

753 posts
  • Sold between 10 000 and 50 000 dollars
  • Referred between 10 and 49 users
  • Author had a Free File of the Month
  • Exclusive Author
  • Has been a member for 5-6 years
  • Bought between 1 and 9 items
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
AdamGold says
not AUTO INCREMENT but AUTO _INCREMENT , and try to global $wp_roles too
<?php /*
 * Plugin Name: plugin name
 * Description: bla bla bla
 * Version: 1.0
 * Author: Author
 */

require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
global $wpdb , $wp_roles;

register_activation_hook(__FILE__, 'flashplayer_install');

function flashplayer_install(){

    global $wpdb;

    $tbl_name = $wpdb?>prefix.'flashplayer';

    if( $wpdb->get_var("show tables like '$tbl_name'") != $tbl_name ){

        $sql = "CREATE TABLE ".$tbl_name." (
                    id INT(10) NOT NULL AUTO_INCREMENT,
                    title TINYTEXT NOT NULL,
                    filename VARCHAR(100) NOT NULL,
                    UNIQUE KEY id(id)
                );";

        dbDelta($sql);

    }

}

?>
good luck :)

Nice catch, didn’t notice that :D I don’t think he needs the global variables outside of the function unless he use them, because right now his only need of them is inside the function.

715 posts
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Belgium
  • Has been a member for 5-6 years
  • Bought between 10 and 49 items
Hy-Studio says

Hot damn, how could I overlook this, it was indeed AUTO _INCREMENT and not AUTO INCREMENT !

$wp_roles wasn’t even necessary.

Thanks!!

by
by
by
by
by
by