私は、しばらくの間公開されていた 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' );