add/get/delete/update_option()不能在激活钩子(wordpress)中工作

问题描述:

我正在更新一段已经出来的WordPress插件,并且插件的“未来打样”摆脱了插件开始阶段实施的一些伪劣命名方案。add/get/delete/update_option()不能在激活钩子(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

阅读本关于register_activation_hook和全局变量:http://codex.wordpress.org/Function_Reference/register_activation_hook#A_Note_on_Variable_Scope – 2012-06-30 17:56:34

下面的代码是什么,当我需要更新自己的插件一些“以次充好命名方案”我建了一个总结:)

我在WordPress StackExchange中询问this question之后构建了我的解决方案。完整的线程是值得一读的。

class MyPlugin { 
    var $adminOptionsName = "MyPlugin"; 

    function MyPlugin() { 
    } 

    function init() { 
     $this->getAdminOptions(); 
    } 

    function getAdminOptions() { 

     // New options and values 
     $theNewOptions = array(
      'option_1' => 0, 
      'option_2' => '', 
      'version' => '1.0' 
     ); 

     // Grab the options in the database 
     $theOptions = get_option($this->adminOptionsName); 

     // Check if options need update 
     if(!isset($theOptions['version']) && !empty($theOptions)) { 
      foreach($theOptions as $key => $value) { 
       if($key == 'not_needed') { 
        unset($theOptions[$key]); 
       } 
       if($key == 'old_option_1') { 
        $theOptions['option_1'] = $value; 
        unset($theOptions[$key]); 
       } 
       // etc... 
      } 
     } 

     // Proceed to the normal Options check 
     if (!empty($theOptions)) { 
      foreach ($theOptions as $key => $option) { 
       $theNewOptions[$key] = $option; 
      } 
     } 

     update_option($this->adminOptionsName, $theNewOptions); 

    } 
}