Problem creating a table with dbDelta

I’m working on a plugin that was creating a table successfully.

However, I decided to test it in a fresh install of WordPress (on my local server) and now the table is not being created in my activation code. Instead I am getting the error message:

Read More

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

Here is my code:

function nc_create_location_table() {
    // makes the location table
    global $wpdb;
    global $simple_location_version;

    $table_name = $wpdb->prefix . "nc_location";

    $installed_ver = get_option( "simple_location_version" );

    if( $installed_ver != $simple_location_version ) {
        $sql = "CREATE TABLE " . $table_name . " (
                location_id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
                PRIMARY KEY  (location_id),
                    name VARCHAR (100),
                    street_no VARCHAR (5),
                    street_name VARCHAR (75),
                    city VARCHAR (75),
                    province_state VARCHAR (75),
                    postal_code VARCHAR(10),
                    country VARCHAR (75)
            );";

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

        update_option("simple_location_version", $simple_location_version);

    }

    $tmp = get_option('nc_location_options');

    if(($tmp['chkbox_reset']=='on')||(!is_array($tmp))) {
    echo 'setting defaults';
        $arr = array(
            "nc_location_zoom"=>10, 
            "nc_location_width" => "200", 
            "nc_location_height" => "200", 
            "nc_location_drop_down_maptype" => "road",
        );
        update_option('nc_location_options', $arr);
    }
}

register_activation_hook(__FILE__,'nc_create_location_table');

When I check my tables, I see that my table hasn’t been created, and also I am getting an error message.

If anyone can shine a light on why code that was working is now not working (and also, is it possible to see where the unexpected output is…as there’s no display as to what the error is other than this message)?

Related posts

Leave a Reply

3 comments

  1. if( $installed_ver != $simple_location_version ) {
    

    on first run, $installed_ver is an empty string and $simple_location_version is NULL, so this inequality test will fail and your SQL will never be executed.

    if you check for strict inequality, it will work:

    if( $installed_ver !== $simple_location_version ) {
    
  2. To answer the

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

    Every echo statement will bring this up. Normally it’s just the count of characters you echo-ed.


    Here is a link to my uninstall/activation/deactivation (plain/vanilla) class for plugins. You can take a look at how I output errors into admin_notices to inspect behavior. You could try it with $this->error( "DB-Error: {$wpdb->last_error}", true ); (or take a look at the wpdb core file, I might be wrong with the object var) to see what’s happening after the dbDelta call. Anyway: Don’t echo and you won’t get the notice/error/unexpected character & header sent message.

  3. Another crude way of investigating the “unexpected output” error is to execute exit() on line 1569 of wp-admin/includes/upgrade.php. It is called at the end of the dbDelta function, which will allow you to see the output that was generated and quickly fix the problem, assuming the problem is with dbDelta. I’ve had difficulties similar to your when I had something going wrong with the SQL for the dbDelta function and this strategy helped debug it. It’s not the greatest solution, but it can help in some situations.