2

This was really driving me crazy. I was debugging some code with code-generated transient names and they were failing like crazy for no apparent reason.

After much much pain and experimentation I figured out that it fails when over certain key length:

$key = '1234567890';
var_dump( get_transient($key) ); // works just fine
var_dump( set_transient( $key, $key, 10) ); // false when not expired


$key = '1234567890123456789012345678901234567890123456';
var_dump( get_transient($key) ); // always returns false
var_dump( set_transient( $key, $key, 10) ); // always true

Apparently option_name field in database is varchar(64).

However I absolutely can't figure out why this doesn't produce any database errors and all returns and hooks on setting transient work just fine... Except that in reality timeout option doesn't get written and it all fails miserably.

I suspect this query in add_option():

$result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $value, $autoload ) );

Can anyone help figure out what it should do when option's name passed is too long and why it doesn't produce errors or anything?

4 Answers 4

9

You don't get an error because WordPress does not check for the length, and MySQL silently truncates it (giving a warning, not an error), unless you enable the STRICT_ALL_TABLES option (which will change the warning into an error).

Even more confusing, when you enable multisite the options are saved in the sitemeta table with a maximum key length of 255, but without multisite they go to options where the maximum key length is 64. Have fun debugging that in your plugin!

2
  • I think site meta only applies to add_site_option() function, transients and regular options still stored as usual. My example code fails on MS installation, but key is much less than 255 characters.
    – Rarst
    Commented Jan 4, 2011 at 17:23
  • @Rarst: Indeed, I probably read that too fast. And by the way: most of my answer comes from following the links scribu and goldenapples provided, only the MySQL part is something I knew by myself.
    – Jan Fabry
    Commented Jan 5, 2011 at 11:18
6

I ran into this exact issue and proposed extending the column length:

http://core.trac.wordpress.org/ticket/13310

1
  • 3
    I am not as worried about length of field (someone will run out of 255 just as well) as that it fails completely silently and with returns completely consistent with normal operation. It would be few seconds to take length in account it I just got error/warning thrown at me, instead I had wasted a lot of time to debug.
    – Rarst
    Commented Jan 4, 2011 at 17:21
2

Very weird behavior. I'm guessing that the option is being added, but its name is truncated to 64 characters? That way $wpdb->rows_affected returns 1, as it should for this check to pass at the end of update_option:

$wpdb->update($wpdb->options, array('option_value' => $newvalue), array('option_name' => $option_name) );

if ( $wpdb->rows_affected == 1 ) {
   do_action( "update_option_{$option_name}", $oldvalue, $_newvalue );
   do_action( 'updated_option', $option_name, $oldvalue, $_newvalue );
   return true;
}

But the option isn't accessible through get_option because the option_name field is not equal to $key.

Extending the column length is one solution, but it seems that there should be an additional check at the end of update_option that the row affected matches the key provided. And possibly the same with other update functions like update_post_meta, although the 255 char limit there is much more reasonable.

1
0

You can update the table and increase the row size?

1
  • 2
    Possibly, but as per explanation in other comments I mostly have issue with silent fail, rather than length itself.
    – Rarst
    Commented Jan 4, 2011 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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