我已经在管理面板上设置了我的插件选项
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
\'my_option_group\', // Option group
\'write_here_options\', // Option name
array( $this, \'sanitize\' ) // Sanitize
);
add_settings_section(
\'setting_section_id\', // ID
\'Set Edit Page\', // Title
array( $this, \'print_section_info\' ), // Callback
\'write-here-setting\' // Page
);
add_settings_field(
"pid_num",
"Select Page >>",
array( $this, \'wh_select_list\' ),
"write-here-setting",
"setting_section_id"
);
add_settings_field(
\'num_of_posts\', // ID
\'Number of Posts to show\', // Title
array( $this, \'num_of_posts_callback\' ), // Callback
\'write-here-setting\', // Page
\'setting_section_id\' // Section
);
}
因此,在DB中,我的插件设置保存在
wp_options
列名称下的表
option_name
像
write_here_options
以我的情况为对象。
当人们激活插件时,我想在DB中保存默认值pid_num => 0
和num_of_posts => 10
.
我该怎么做??
SO网友:s_ha_dum
您的代码肯定会使用get_option()
检索选项的值。get_option()
接受允许您指定默认值的第二个参数。使用它,而不是不必要地将值插入数据库。
get_option( $option, $default );
如果您担心第三方代码,那么
option_{$option}
filter 即使在那时,您也应该能够使用它来保留默认值:
116 /**
117 * Filter the value of an existing option.
118 *
119 * The dynamic portion of the hook name, `$option`, refers to the option name.
120 *
121 * @since 1.5.0 As \'option_\' . $setting
122 * @since 3.0.0
123 *
124 * @param mixed $value Value of the option. If stored serialized, it will be
125 * unserialized prior to being returned.
126 */
127 return apply_filters( \'option_\' . $option, maybe_unserialize( $value ) );