The MU forums have moved to WordPress.org

How to store arrays using update_blog_option (5 posts)

  1. beaulebens
    Member
    Posted 16 years ago #

    In case you're having difficulties doing something custom (as I have been!), then you might like to know this.

    It appears that if you're using the WP Object Cache (define('ENABLE_CACHE', true); define('WP_CACHE', true); in your wp-config.php) then you need to serialize arrays *before* passing them to update_blog_option($id, $array);

    As demonstrated by a quick test;

    <?php
    require_once 'wp-blog-header.php';
    $foo = array('this'=>'that', 'foo'=>'bar');
    update_blog_option(2, 'serial_foo', serialize($foo));
    print_r(get_blog_option(2, 'serial_foo'));
    update_blog_option(2, 'foo', $foo);
    print_r(get_blog_option(2, 'foo'));
    ?>

    This appears to be because the cache-handling code automatically does some serialization/unserialization - I haven't looked too deeply into it, just figured out that this was required to make it work.

    HTH someone.

    Beau

  2. beaulebens
    Member
    Posted 16 years ago #

    As a follow up to this, it appears as if once the cache expires, then the value you get back is not unserialized? Something appears to be whacky here - can anyone else confirm that there's something strange going on here?

    I would assume that as per set_usermeta(), update_blog_option() should *not* require serialization, and obviously when it comes back out, it should be an actual array or whatever automatically...

  3. wep
    Member
    Posted 16 years ago #

    i am add enable_cache in wp-config.php,true and define('ENABLE_CACHE', true); define('WP_CACHE', true) ok but

    <?php
    require_once 'wp-blog-header.php';
    $foo = array('this'=>'that', 'foo'=>'bar');
    update_blog_option(2, 'serial_foo', serialize($foo));
    print_r(get_blog_option(2, 'serial_foo'));
    update_blog_option(2, 'foo', $foo);
    print_r(get_blog_option(2, 'foo'));
    ?>

    ENABLED CACHE is automatic or no? because with add in wp-config.php ok or not?

  4. lunabyte
    Member
    Posted 16 years ago #

    define('WP_CACHE', true);
    That's only if you're using the WP Cache plugin.

    define('ENABLE_CACHE', true);
    That's for the internal object caching.

  5. beaulebens
    Member
    Posted 16 years ago #

    So after playing with this for a while, basically I figured out that the only safe way to handle arrays as blog options was to *always* maybe_serialize() them when you're storing them, and then maybe_unserialize() them when you retrieve them. If you don't do that (and have the object cache enabled) then you'll have problems.

About this Topic

  • Started 16 years ago by beaulebens
  • Latest reply from beaulebens