1
我正在寫一個WordPress插件,它使用Settings API來註冊一些選項。當我嘗試保存我的選項時,他們不保存。當頁面重新加載並且沒有保存選項的消息時,這些字段爲空。我一直在琢磨幾個小時,找不到任何答案。我將非常感謝任何幫助。這裏是(略有修改)開始我的選項頁面代碼:wordpress主題選項不保存
<?php
/**************************************** Register Settings Menu & Page ****************************************/
add_action('admin_menu', 'rv_admin_menu');
function rv_admin_menu() {
// Parent Slug, Page Title, Menu Title, Capability, Menu Slug, Callback Function
add_submenu_page('slug', 'Misc. Options', 'Misc. Options', 'manage_options', 'rv-options-page', 'rv_options_create_page');
}
/**************************************** Register Settings ****************************************/
add_action('admin_init', 'rv_admin_init');
function rv_admin_init() {
/** Add the Settings Group **/
register_setting('rv-options-group', 'rv-settings'); // Option Group & Options Name
/* Add Custom Footer Section & Fields */
// ID, Title, Callback, Menu Page
add_settings_section('footer-section', 'Custom Footer', 'custom_footer_section_callback', 'rv-options-page');
// ID, Title, Callback, Menu Page, Associated Section
add_settings_field('footer-content', 'Footer Content', 'custom_footer_callback', 'rv-options-page', 'footer-section');
/* Add Author Box Section & Fields */
add_settings_section('author-box', 'Author Box', 'author_box_section_callback', 'rv-options-page');
add_settings_field('author-box-enable', 'Globally Enable Author Box', 'author_box_enable_callback', 'rv-options-page', 'author-box');
add_settings_field('author-box-title', 'Author Box Title', 'author_box_title_callback', 'rv-options-page', 'author-box');
}
/**************************************** Footer ****************************************/
/** Custom Footer Section Intro **/
function custom_footer_section_callback() {
echo "Add your footer's custom HTML.";
}
/* Custom Footer Text Area */
function custom_footer_callback() {
$settings = (array) get_option('rv-settings');
$footer = esc_attr($settings['footer']);
echo "<textarea rows='12' cols='100' name='rv-settings[footer]' value='$footer'></textarea>";
}
/**************************************** Author Box ****************************************/
/** Author Box Section Intro **/
function author_box_section_callback() {
echo "Customize the Author Box";
}
/* Globally Enable Author Box on Posts */
function author_box_enable_callback() {
$settings = (array) get_option('rv-settings');
$enable_author_box = esc_attr($settings['enable_author_box']);
echo "<input type='checkbox' name='rv-settings[enable_author_box]' value='$enable_author_box' />";
}
/* Custom Author Box Title */
function author_box_title_callback() {
$settings = (array) get_option('rv-settings');
$author_box_title = esc_attr($settings['author_box_title']);
echo "<input size='100' type='text' name='rv-settings[author_box_title]' value='$author_box_title' />";
}
/**************************************** Create Settings Page ****************************************/
function rv_options_create_page() { ?>
<div class="wrap">
<h2>RV Options</h2>
<form action="options.php" method="POST">
<?php settings_fields('rv-options-group'); // Options Group ?>
<?php do_settings_sections('rv-options-page'); // Menu Page ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}