1
我在爲wordpress添加自定義設置頁面時遇到了問題,並且更具體地獲取了要保存的選項。未保存Wordpress自定義選項
此文檔似乎缺乏和不一致,因爲不同的示例和不同的功能以不同的方式解釋相同的事情。
wordpress codex上的最新示例使用sanitize回調函數來保存選項。但add_settings_field()文檔說,保存選項應該在幕後進行。似乎更奇怪的是使用san回調保存反正。
我已經嘗試了各種不同的方法,並在一次我的代碼設法保存一個領域,但不是所有的領域。
我當前的代碼:
class wwtkSettings {
public function __construct() {
if (is_admin()){
add_action('admin_menu', array($this, 'add_plugin_page'));
add_action('admin_init', array($this, 'page_init'));
}
}
public function add_plugin_page(){
add_options_page('WWTK Settings', 'WWTK settings', 'manage_options', 'wwtk-settings', array($this, 'create_admin_page'));
}
public function create_admin_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>WWTK Settings</h2>
<form method="post" action="options.php">
<?php
settings_fields('wwtk-setting-group');
do_settings_sections('wwtk-settings');
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function page_init() {
register_setting('wwtk-setting-group', 'wwtk-setting-group', array($this, 'wwtk_validate_options'));
add_settings_section(
'wwtk-settings-section',
'Categories and pages',
array($this, 'print_section_info'),
'wwtk-settings'
);
add_settings_field(
'frontpage',
'Frontpage category:',
array($this, 'create_category_field'),
'wwtk-settings',
'wwtk-settings-section',
'frontpage'
);
add_settings_field(
'category1',
'Category & page 1',
array($this, 'create_category_field'),
'wwtk-settings',
'wwtk-settings-section',
'category1'
);
add_settings_field(
'category2',
'Category & page 2',
array($this, 'create_category_field'),
'wwtk-settings',
'wwtk-settings-section',
'category2'
);
add_settings_field(
'category3',
'Category & page 3',
array($this, 'create_category_field'),
'wwtk-settings',
'wwtk-settings-section',
'category3'
);
}
//TODO: check if category and page exist. and sanitize!
public function wwtk_validate_options($input) {
return $input;
}
public function print_section_info(){
print 'Enter the names of categories that should be displayed on the page with the same name(both must exist):';
}
public function create_category_field($args){
?><input type="text" id="<?php echo $args ?>" name="wwtk-setting-group" value="<?php echo get_option('wwtk-setting-group['.$args.']'); ?>" /><?php
}
}
是這一切都爲你的插件的代碼?我正在爲您制定一個解決方案,但要確保我有所有必要的代碼來幫助;) – Ian
這是一個自定義主題,除了添加導航菜單和添加對縮略圖的支持外,這是整個功能。 PHP。 非常感謝您試圖幫助:) – Jeger
重點是利用保存的選項在每個頁面上顯示特定類型的帖子。我想嘗試不同的方法,但試圖保存多個自定義選項時卡住了。 – Jeger