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.

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?

share|improve this question
add comment

4 Answers

up vote 8 down vote accepted

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!

share|improve this answer
    
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 Jan 4 '11 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 Jan 5 '11 at 11:18
add comment

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

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

share|improve this answer
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 Jan 4 '11 at 17:21
add comment

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.

share|improve this answer
    
Oh, should have researched first. There's a trac ticket for that also: core.trac.wordpress.org/ticket/15058 –  goldenapples Jan 4 '11 at 1:39
add comment

You can update the table and increase the row size?

share|improve this answer
2  
Possibly, but as per explanation in other comments I mostly have issue with silent fail, rather than length itself. –  Rarst Jan 4 '11 at 17:24
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.