Long option names fail silently?

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

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!

Leave a Comment