715 posts
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Belgium
+1 more
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
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Had an item featured on Envato Market
+4 more
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
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Belgium
+1 more
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
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Had an item featured on Envato Market
+4 more
AdamGold says

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

6 posts
  • Has been part of the Envato Community for over 4 years
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
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
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Had an item featured on Envato Market
+4 more
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
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Belgium
+1 more
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!!

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by