如何将缺省值保存到选项中,并在WordPress中激活和停用时删除这些值?

时间:2015-07-14 作者:defiant

我使用设置API保存和删除我创建的选项中的值!但它不起作用。有人能告诉我为什么它不起作用吗?代码如下:

register_activation_hook( __FILE__, \'twmm_activate\'); //activate plugin options open activation of plugin
register_deactivation_hook(__FILE__, \'twmm_deactivate\'); //deactivate plugin options open deactivation of plugin
function twmm_activate() {
  $defaults_twmm = array(
    \'twmm_on_off\' => \'disabled\',
    \'twmm_middle_layout_selector\' => \'email\',
  );
  foreach ($defaults_twmm as $key => $value) {
    update_option($key, $value);
  }
}
function twmm_deactivate() {
  delete_option(\'twmm_options\'); //delete plugin specific options upon deactivation
}

1 个回复
SO网友:defiant

以下代码有效。根据米洛的评论,我使用选项名称是错误的。因此,我使用选项中的每个键,并使用数组传递默认值。谢谢你,米洛!:_

register_activation_hook( __FILE__, \'twmm_activate\'); //activate plugin options open activation of plugin
register_deactivation_hook(__FILE__, \'twmm_deactivate\'); //deactivate plugin options open deactivation of plugin
function twmm_activate() {
  $defaults_twmm = array(
    \'twmm_on_off\' => \'disabled\',
    \'twmm_middle_layout_selector\' => \'email\',
    \'twmm_permission_lvl\' => \'admin\',
    \'twmm_page_title\' => \'We will be back\',
    \'twmm_custom_message\' => \'Please check back after some time. <br />Thank you!\',
    \'twmm_facebook\' => \'http://facebook.com/techtuft\',
    \'twmm_twitter\' => \'http://twitter.com/techtuft\',
  );
  $options = wp_parse_args(update_option(\'twmm_options\', $defaults_twmm));
}
function twmm_deactivate() {
  delete_option(\'twmm_on_off\'); //delete plugin specific options upon deactivation
  delete_option(\'twmm_middle_layout_selector\');
  delete_option(\'twmm_permission_lvl\');
  delete_option(\'twmm_page_title\');
  delete_option(\'twmm_custom_message\');
  delete_option(\'twmm_facebook\');
  delete_option(\'twmm_twitter\');
}

结束