我正在更新已经出现一段时间的WordPress插件,而插件的“未来验证”的一部分是摆脱在插件的开始阶段实现的一些劣质命名方案 .
我想我只需添加一个激活钩子,它会检查是否存在任何这些名称,如果是这样,只需删除插件选项数组并显示一条通知,让用户知道他们需要再次更新他们的选项,因为我们不得不擦洗数据以备将来验证 . 听起来很容易,是吗?显然不是 .
我已经尝试了所有可能的方法,无论我做什么,我都无法在激活钩子内部使用delete_option(),update_option(),add_option()和get_option() .
下面是我的激活挂钩的设置示例:
// add activation function to get rid of old options and services
function my_activation_hook() {
global $opts_array;
//Set variable to false before running foreach loop
$needs_update = false;
//Loop through $opts_array['bookmark'] and check for 'badname-' prefix
foreach($opts_array['bookmark'] as $bkmrk) {
if(strpos($bkmrk, 'badname-') !== false) {
//If any have 'badname-' prefix, set variable to true
$needs_update = true;
}
}
//If variable is true, delete options and set to default
if($needs_update === true) {
//Delete the options
unset($opts_array);
delete_option('MyPluginOpts');
//Reset the array to default values
$opts_array = array(
'position' => 'below', // below, above, or manual
'reloption' => 'nofollow', // 'nofollow', or ''
'targetopt' => '_blank', // 'blank' or 'self'
'bgimg-yes' => 'yes', // 'yes' or blank
'mobile-hide' => '', // 'yes' or blank
'bgimg' => 'shr', // default bg image
'shorty' => 'b2l',
'pageorpost' => '',
'bookmark' => array_keys($bookmarks_opts_data), // pulled from bookmarks-data.php
'feed' => '1', // 1 or 0
'expand' => '1',
'autocenter' => '1',
'ybuzzcat' => 'science',
'ybuzzmed' => 'text',
'twittcat' => '',
'tweetconfig' => '${title} - ${short_link}', // Custom configuration of tweet
'defaulttags' => 'blog', // Random word to prevent the Twittley default tag warning
'warn-choice' => '',
'doNotIncludeJQuery' => '',
'custom-mods' => '',
'scriptInFooter' => '1',
'vernum' => 'old', //Set to "old" to trigger update notice
);
add_option('MyPluginOpts', $opts_array); // Store the option to the database wp_options table in a serialized array
$opts_array = get_option('MyPluginOpts');// Now reload the variable with stored options from database
}
}
register_activation_hook(__FILE__, 'my_activation_hook' );
1 回答
下面的代码是我在我自己的插件中更新一些“劣质命名方案”时构建的内容的摘要:)
在WordPress StackExchange中询问this question后,我已经构建了我的解决方案 . 完整的线程值得一读 .