使用《法典》中的示例Settings API 部分Put it 在主题的functions.php
:
add_action(\'admin_init\', \'eg_settings_api_init\');
function eg_settings_api_init() {
add_settings_section(
\'eg_setting_section\',
\'Example settings section in reading\',
\'eg_setting_section_callback_function\',
\'reading\'
);
add_settings_field(
\'eg_setting_name\',
\'Example setting Name\',
\'eg_setting_callback_function\',
\'reading\',
\'eg_setting_section\'
);
register_setting(\'reading\',\'eg_setting_name\');
}
function eg_setting_section_callback_function() {
echo \'<p>Intro text for our settings section</p>\';
}
function eg_setting_callback_function() {
echo \'<input name="eg_setting_name"
id="gv_thumbnails_insert_into_excerpt"
type="checkbox"
value="1"
class="code" \'
. checked( 1, get_option(\'eg_setting_name\'), false )
. \' />Explanation text\';
}
这将在中创建一个复选框
Settings > Reading
:
在主题中header.php
文件,它可用于呈现一个红色的大警告横幅:
<?php
if( get_option(\'eg_setting_name\') ) {
echo \'<h1 style="background-color: #fcc;width:100%;height:3em;line-height:3em;text-align:center;top:40px;position:fixed">
Warning!
</h1>\';
}
?>