Limitations for Option Strings in WordPress

Are there limitations for strings used for options? I’m tyring to use the Transient API and when I inlude a string of url in the transient name, it does not save the data. I’m not sure if it is due to a character length or illegual characters.

$url = 'http://stackoverflow.com/questions/tagged/php+wordpress+wordpress-plugin';
$strTransient = 'sample_transient_' . $url;
$key = 'sample_transient';
$html = get_transient($strTransient);   
if( false === $html ) {
    echo 'cache is not used: ' . $strTransient . '<br />';      
    $html = wp_remote_get($url);    
    $html = $html['body'];  
    $savehtml = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $html, MCRYPT_MODE_CBC, md5(md5($key))));
    set_transient($strTransient, $savehtml, 60 );
} else {
    echo 'cache is used. <br />';
    $html = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($html), MCRYPT_MODE_CBC, md5(md5($key)));  
}
print_r($html);

Thanks in advance.

Read More

[Edit]

It seems to be due to the character length.

$transientkey = 'verylongstring_verylongstring_verylongstring_verylongstring_verylongstring_verylongstring_verylongstring'; // <-- fails
// $transientkey = 'shortstring';   // <-- okay

$data = get_transient($transientkey);   
if( false === $data ) {
    echo 'transient is not saved: ' . $transientkey . '<br />';     
    $data = 'hello world!';
    echo 'now saving the data.<br />';
    set_transient($transientkey, $data, 60 );
} else {
    echo 'transient is used. <br />';
}
print_r($data);

So where can I find the exact information about the limitations for option keys? I could not find it in the core. This is the comment for set_transient()

wp-includes/option.php

/**
 * Set/update the value of a transient.
 *
 * You do not need to serialize values. If the value needs to be serialized, then
 * it will be serialized before it is set.
 *
 * @since 2.8.0
 * @package WordPress
 * @subpackage Transient
 *
 * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
 *  transient value to be stored.
 * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
 *
 * @param string $transient Transient name. Expected to not be SQL-escaped.
 * @param mixed $value Transient value. Expected to not be SQL-escaped.
 * @param int $expiration Time until expiration in seconds, default 0
 * @return bool False if value was not set and true if value was set.
 */

Also any suggestion to identify a url as a transient key?

Related posts

Leave a Reply

1 comment

  1. The documentation says the transient name is “Expected to not be SQL-escaped. Should be 45 characters or less in length.

    Looking at the table structure in WordPress 3.4.1:

    mysql> desc wp_options;
    +--------------+---------------------+------+-----+---------+----------------+
    | Field        | Type                | Null | Key | Default | Extra          |
    +--------------+---------------------+------+-----+---------+----------------+
    | option_id    | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
    | option_name  | varchar(64)         | NO   | UNI |         |                |
    | option_value | longtext            | NO   |     | NULL    |                |
    | autoload     | varchar(20)         | NO   |     | yes     |                |
    +--------------+---------------------+------+-----+---------+----------------+
    4 rows in set (0.00 sec)
    

    So the option_name can be no longer than 64 characters, but the data can be as large as 232 bytes (~4GB). set_transient ultimately calls add/update_option which can have an option name up to 64 characters long, but since the transient works in a special way, 45 is the longest value you should use.

    EDIT:

    From looking at the WP code, the reason the transient name is restricted to 45 characters per the reference guide is because the code does this:

    $transient_timeout = '_transient_timeout_' . $transient;
    //...
    add_option( $transient_timeout, time() + $expiration, '', 'no' );
    

    So if you pass hello as the transient, it will add an option called _transient_timeout_hello. Given that: 45 + strlen('_transient_timeout_') === 64 and 64 is the maximum varchar length of the option name field in the WP options table.