Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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)?

share|improve this question
1  
Try to remove the extra space between PRIMARY KEY and (location_id). dbDelta() does some very nasty regex processing and it might fail due to extra spacing characters in places where it doesn't expect them... –  onetrickpony Aug 21 '11 at 15:19
1  
Also adding echo $wpdb->last_error; after dbDelta might help you find out what's the actual error –  onetrickpony Aug 21 '11 at 15:25
    
Will try the last remark, but I have a question about the first...the codex says to put TWO spaces between PRIMARY KEY and the definition of my primary key (location_id) hence the extra spacing...is the codex just out of date you think (it's possible) codex.wordpress.org/Creating_Tables_with_Plugins –  redconservatory Aug 22 '11 at 12:42
add comment

3 Answers

up vote 4 down vote accepted
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 ) {
share|improve this answer
add comment

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.

share|improve this answer
    
I only echo 'setting defaults' in the callback for register_activation_hook...isn't this the first callback when the plugin is activated? What else is getting echoed then? –  redconservatory Aug 22 '11 at 12:40
    
You shouldn't echo anything. Any output will send headers, because you get a redirect. Then stuff drops the "error" in 'admin_notices'. This stops the plugin in some cases. I did some tests last week about this and came up with: see link in updated Q. –  kaiser Aug 22 '11 at 15:12
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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