您可以使用WordPress settings api
下面是一个简单的示例,它将创建一个输入字段,供用户输入自定义徽标的路径。
// Add a menu for our option page
add_action(\'admin_menu\', \'prefix_myplugin_add_page\');
function prefix_myplugin_add_page() {
add_options_page( \'My Theme Options\', \'Theme Name Options\', \'manage_options\', \'prefix_mytheme\', \'prefix_myplugin_option_page\' );
}
// Draw the option page
function prefix_mytheme_option_page() {
?>
<div class="wrap">
<h2>Really Simple Google Analytics</h2>
<form action="options.php" method="post">
<?php settings_fields( \'prefix_mytheme_options\' ); ?>
<?php do_settings_sections( \'prefix_mytheme\' ); ?>
<input name="Submit" type="submit" value="Save Changes" />
</form>
</div>
<?php
}
// Register and define the settings
add_action( \'admin_init\', \'prefix_mytheme_admin_init\' );
function prefix_mytheme_admin_init(){
register_setting(
\'prefix_mytheme_options\',
\'prefix_mytheme_options\'
);
add_settings_section(
\'prefix_mytheme_main\',
\'prefix_mytheme_options\',
\'prefix_mytheme_section_text\',
\'prefix_mytheme\'
);
add_settings_field(
\'prefix_mytheme_text_string\',
\'Enter text here\',
\'prefix_mytheme_setting_input\',
\'prefix_mytheme\',
\'prefix_mytheme_main\'
);
}
// Draw the section header
function prefix_mytheme_section_text() {
echo \'<p>Enter path to custom logo.</p>\';
}
// Display and fill the form field
function prefix_mytheme_setting_input() {
// get option \'text_string\' value from the database
$options = get_option( \'prefix_mytheme_options\' );
$text_string = $options[\'text_string\'];
// echo the field
echo "<input id=\'text_string\' name=\'prefix_mytheme_options[text_string]\' type=\'text\' value=\'$text_string\' />";
}
在标题中调用自定义徽标选项。php文件:
$options = get_option( \'prefix_mytheme_options\' );
if ( ! empty( $options[\'text_string\'] ) ) {
$logo = $options[\'text_string\'];
} else { $logo = \'http://path_to_default_logo\';
}