The plugin generated x characters of unexpected output, $wpdb not defined

I’ve written a simple WordPress plugin to create a new database table.
The new table ought to be created when the plugin is activated.
When I try to activate the plugin, I get the following error:

The plugin generated 3989 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.

emphasized text
This is obviously a result of the fact the $wbdb is not defined. xDebug outputs the following:

Read More
[Mon Feb 04 ] [error] PHP Notice:  Undefined variable: wpdb in test.php on line 13

The entire plugin consists of the following:

<?php

/**
 * Plugin Name: Test Plugin
 * Plugin URI: http://everybytcaptive.com
 * Description: A test plugin.
 * Version: 1.0
 * Author: Christopher Green
 * Author URI: http://everybytecaptive.com
 */

$test_db_name = $wpdb->prefix . 'test_db_name';

function test_install_plugin() {
    global $wpdb;
    global $test_db_name;

    $sql = "CREATE TABLE " . $test_db_name . " (
        `id` int(9) NOT NULL AUTO_INCREMENT,
        UNIQUE KEY id (id)
    );";

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

register_activation_hook(__FILE__,'test_install_plugin');

?>

I have no other plugins installed.
Why isn’t $wpdb defined?
Is there a standard way to create a new database table when activating a plugin?

Related posts

Leave a Reply

3 comments

  1. Your plugin is trying to access $wpdb before it exists. You need to wrap it in an action hook, like so:

    <?php
    
    /**
     * Plugin Name: Test Plugin
     * Plugin URI: http://everybytcaptive.com
     * Description: A test plugin.
     * Version: 1.0
     * Author: Christopher Green
     * Author URI: http://everybytecaptive.com
     */
    
    add_action( 'init', 'test_install_plugin' );
    
    function test_install_plugin() {
        global $wpdb;
        global $test_db_name;
    
        $test_db_name = $wpdb->prefix . 'test_db_name';
    
        $sql = "CREATE TABLE " . $test_db_name . " (
            `id` int(9) NOT NULL AUTO_INCREMENT,
            UNIQUE KEY id (id)
        );";
    
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }
    
    // commented out for now, worry about running activation later
    // register_activation_hook(__FILE__,'test_install_plugin');
    

    I highly recommend devouring everything you can find with Google relating to actions and filters in WordPress. They are core concepts that you need to fully understand in order to create plugins and themes effectively.

  2. Well, in my case the issue was due to a duplicate db_table creation process, and when I added an if statement to check whether the db_table exists or not the issue was resolved.

    Here is how:

    define( 'TABLEName', $wpdb->prefix . 'test_table' );
    
    if( $wpdb->get_var( "show tables like '" . TABLEName . "'" ) != TABLEName ) {
    create table;
    }
    

    Now the plugin checks if the required table(s) exists or not before activation, if not exist then it will create it, if exists will escape the creation.