問題描述
我想做的是將一些自定義字段添加到常規設置。這是我使用的代碼。它的工作正常,但我只是想不到如何添加更多的字段。
我現在要創建兩個字段,一個是電話號碼,另一個用於地址:
function register_fields()
{
register_setting('general', 'my_first_field', 'esc_attr');
add_settings_field('my_first_field', '<label for="my_first_field">'.__('My Field' , 'my_first_field' ).'</label>' , 'print_custom_field', 'general');
}
function print_custom_field()
{
$value = get_option( 'my_first_field', '' );
echo '<input type="text" id="my_first_field" name="my_first_field" value="' . $value . '" />';
}
add_filter('admin_init', 'register_fields');
我設法讓它在多個領域工作的唯一方法是複製一切。
那麼這樣看起來就像這樣:
function register_fields()
{
register_setting('general', 'my_first_field', 'esc_attr');
add_settings_field('my_first_field', '<label for="my_first_field">'.__('My Field' , 'my_first_field' ).'</label>' , 'print_first_field', 'general');
register_setting('general', 'my_second_field', 'esc_attr');
add_settings_field('my_second_field', '<label for="my_second_field">'.__('My Field' , 'my_second_field' ).'</label>' , 'print_second_field', 'general');
}
function print_first_field()
{
$value = get_option( 'my_first_field', '' );
echo '<input type="text" id="my_first_field" name="my_first_field" value="' . $value . '" />';
}
function print_second_field()
{
$value = get_option( 'my_second_field', '' );
echo '<input type="text" id="my_second_field" name="my_second_field" value="' . $value . '" />';
}
add_filter('admin_init', 'register_fields');
但這可能不是最好的方法,我嘗試創建一個 settings_section,但它沒有工作或沒有保存等。它只是很混亂。
最佳解決方案
那麼第二位代碼在技術上是正確的方法。但是,在 add_settings_field()的最後,您可以通過爭論。
請查看 WordPress Add_Settings_Field 功能參考。這將幫助您更好地瞭解 add_settings_field()功能如何真正有效。
現在,您可以使用’shared’ 函數進行回調。比如我在我的選項頁面,當我開發主題。
這是我如何做的一個例子。
// My Example Fields
add_settings_field(
'tutorial_display_count',
'Tutorial Display Count',
'ch_essentials_textbox_callback',
'ch_essentials_front_page_option',
'ch_essentials_front_page',
array(
'tutorial_display_count' // $args for callback
)
);
add_settings_field(
'blog_display_count',
'Blog Display Count',
'ch_essentials_textbox_callback',
'ch_essentials_front_page_option',
'ch_essentials_front_page',
array(
'blog_display_count' // $args for callback
)
);
// My Shared Callback
function ch_essentials_textbox_callback($args) {
$options = get_option('ch_essentials_front_page_option');
echo '<input type="text" id="' . $args[0] . '" name="ch_essentials_front_page_option[' . $args[0] . ']" value="' . $options['' . $args[0] . ''] . '"></input>';
}
它將需要一點點的自定義來滿足您的需求,但為您的回調執行共享功能將在代碼方面節省大量空間。除此之外,你正在做正確的事情。
– 編輯 –
好的,這是它應該是什麼樣的你只需修改代碼,根據需要,我寫了這個飛行.. 我做了測試它檢查,它的工作。您只需要修改 add_settings_field 以滿足您的需要。如果您需要添加更多,只需複製並粘貼並編輯它。確保 register_setting 或它將無法正常工作。
add_action('admin_init', 'my_general_section');
function my_general_section() {
add_settings_section(
'my_settings_section', // Section ID
'My Options Title', // Section Title
'my_section_options_callback', // Callback
'general' // What Page? This makes the section show up on the General Settings Page
);
add_settings_field( // Option 1
'option_1', // Option ID
'Option 1', // Label
'my_textbox_callback', // !important - This is where the args go!
'general', // Page it will be displayed (General Settings)
'my_settings_section', // Name of our section
array( // The $args
'option_1' // Should match Option ID
)
);
add_settings_field( // Option 2
'option_2', // Option ID
'Option 2', // Label
'my_textbox_callback', // !important - This is where the args go!
'general', // Page it will be displayed
'my_settings_section', // Name of our section (General Settings)
array( // The $args
'option_2' // Should match Option ID
)
);
register_setting('general','option_1', 'esc_attr');
register_setting('general','option_2', 'esc_attr');
}
function my_section_options_callback() { // Section Callback
echo '<p>A little message on editing info</p>';
}
function my_textbox_callback($args) { // Textbox Callback
$option = get_option($args[0]);
echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" />';
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。