大体上yes, 您以两种格式存储它,作为选项和常量,以便于比较。它提供了一层制衡机制,与一开始不这样做相比,实施起来并不困难真相
请参阅以下问题和说明;一些好例子的线索如何:
Note: none of the code shown here is mine so full credit where credit is due!
问题:Releasing new plugin version, how to rename old options keys?m0r7if3r 提供以下内容https://wordpress.stackexchange.com/a/49736/13418
if( $db_version < {your desired version} ) {
// previous updates and such
$db_version = $new_version; //put that in the database
}
if( $db_version < $current_version ) {
create $options array
foreach( $option as $o ) {
if( get_option( $o[\'old_name\'] ) ) {
update_option( $o[\'new_name\'], get_option( $o[\'old_name\'] ) );
delete_option( $o[\'old_name\'] ); //clean up behind yourself
}
}
and then update your database version again
}
这是对这个问题公认的答案。
然而
One Trick Pony 继续到elaborate on that example with a nice bit of code in response...
class MyPlugin{
const
OPTION_NAME = \'my_plugin_options\',
VERSION = \'1.0\';
protected
$options = null,
// default options and values go here
$defaults = array(
\'version\' => self::VERSION, // this one should not change
\'test_option\' => \'abc\',
\'another_one\' => 420,
);
public function getOptions(){
// already did the checks
if(isset($this->options))
return $this->options;
// first call, get the options
$options = get_option(self::OPTION_NAME);
// options exist
if($options !== false){
$new_version = version_compare($options[\'version\'], self::VERSION, \'!=\');
$desync = array_diff_key($this->defaults, $options) !== array_diff_key($options, $this->defaults);
// update options if version changed, or we have missing/extra (out of sync) option entries
if($new_version || $desync){
$new_options = array();
// check for new options and set defaults if necessary
foreach($this->defaults as $option => $value)
$new_options[$option] = isset($options[$option]) ? $options[$option] : $value;
// update version info
$new_options[\'version\'] = self::VERSION;
update_option(self::OPTION_NAME, $new_options);
$this->options = $new_options;
// no update was required
}else{
$this->options = $options;
}
// new install (plugin was just activated)
}else{
update_option(self::OPTION_NAME, $this->defaults);
$this->options = $this->defaults;
}
return $this->options;
}
}
请不要认为这个答案是正确的,我只是提供额外的支持材料,作为参考,我认为这些材料很好地支持了这个主题。
但是,一定要表示您的支持up-vote 他们的答案if you use them 取得任何成功。