我似乎记得在某处看到过一条提示,说将插件的版本号保存为选项是一种最佳实践。我正在努力发布一个插件,我正在考虑是否这样做,但由于插件所做的只是制作一个小部件(目前,它实际上没有其他选项),我很难理解我将如何使用该选项。我已经用版本号设置了一个常量,以便在几个地方使用(主要是wp_enqueue_*
).
有人能给我指出一个好的资源,或者解释一下将版本号保存为选项的用例吗?
我似乎记得在某处看到过一条提示,说将插件的版本号保存为选项是一种最佳实践。我正在努力发布一个插件,我正在考虑是否这样做,但由于插件所做的只是制作一个小部件(目前,它实际上没有其他选项),我很难理解我将如何使用该选项。我已经用版本号设置了一个常量,以便在几个地方使用(主要是wp_enqueue_*
).
有人能给我指出一个好的资源,或者解释一下将版本号保存为选项的用例吗?
您需要将版本保存到数据库中——aka。将其保存为“选项”--以便脚本具有比较大小写。也就是说,您的脚本应该。。。
检查数据库中的版本号,将该版本号与新版本号(插件文件中的变量或常量)进行比较
正如您所说,如果插件足够简单,可能就没有必要了。
大体上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 取得任何成功。
我想你指的是Wrox WordPress插件书或者Ozh et als博客上的东西。我还刚刚阅读了Apress WordPress插件指南中引用的内容。
其理由是,通过在数据库中记录版本号,然后当您发布插件更新时,您可以更好地控制“更新”内容。。。代码如下所示
DEFINE VERSION (1.0);
function add_settings();
if (get_option(youroptions[VERSION]) === false {
\\\\ ie first time user
add_option(youroptions[version]) = VERSION;
add_option(youroptions[anotheroption]) ;
}
elseif (get_option(youroptions[VERSION]) !=\'1.0\'{
\\\\user has old plugin ... do something for them
add_option(youroptions[optionname])
}
这有帮助吗?我正在使用上面来自Apress WP书籍的代码示例(但我是从内存中键入的)。