开发者

WordPress: Updating individual elements in serialized options data set

In WordPress 3.0 I need to update individual options stored in the wp_options table as serialized data. (for ajax'ed UI)

Since I will just have one key-value pair (not the whole set of options) when I update, I need to manually modify the entire array I have stored in serialized form, and then update the whole thing.

I could not find a native WP function for this, so I do it with the function below, and it works fine.

My question is: Is this the best way, or is there a WP wrapper function for this – or alternatively – should it be done writing a class that extends an existing WP class?

/**
  * Function to fetch, modify and store a serialized options string 
  * Used for updating an individual key-value pair within a larger data set
  * 
  * $opt_group is the name if the option in the wp_options table
  * it contains serialized data representing an array of individual oprions relating
  * to an 'options-group of a theme framework
  */

function alt_update_option($opt_key,$opt_val,$opt_group){   
    // get options-data as it exists before update
    $options = get_option($opt_group);
    // update it
    $options[$opt_key] = $value;
    // store updated data
 开发者_如何转开发   update_option($opt_group,$options);
}


AFAIK there is no way to update a single element of a serialized option, you have to retrieve and send the whole group.

You could perhaps split your options into different wp_options in the database to cut some of the data size overhead, but in any case, you'll still be hitting the database on each request, which is probably going to be the most expensive operation.

Basically if you looking to only modify one element for performance reasons, I wouldn't worry about it. Wordpress does so many things behind the scenes that will probably crush any optimization you manage to achieve. If you're just wanted to do it, sorry, I think it's a no go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜