这是我在为主题定义自定义选项时使用的函数。
要将动态字段添加到自定义选项,请使用以下字段添加到$options数组:
“名称”是将显示在管理面板中的显示。例如“Facebook URL”
“desc”是显示在字段下方的文本。例如“您的 Facebook 页面的完整 HTTP URL”
“id”是您在获取设置值时将在主题中使用的变量。
“类型”是要显示的字段类型。这些选项可以是文本字段的“文本”、文本区域字段的“文本区域”或标题中断的“标题”。标题分隔符可让您对不同的字段进行分类。
只需要“姓名”和“身份证”。
$options =
array(
array("name" => " Options", "type" => "title"),
array("type" => "open"),
array("name" => "", "desc" => "", "id" => "", "type" => "text"),
array( "type" => "close")
);
function mytheme_add_admin() {
global $shortname, $options;
if($_GET['page'] == basename(__FILE__)) {
if($_REQUEST['action'] == 'save') {
foreach($options as $value) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] );
}
foreach($options as $value) {
if(isset($_REQUEST[ $value['id']])) {
update_option($value['id'], $_REQUEST[$value['id']]);
} else {
delete_option( $value['id'] );
}
}
header("Location: themes.php?page=functions.php&saved=true");
die;
} else if($_REQUEST['action'] == 'reset') {
foreach($options as $value) {
delete_option($value['id']);
}
header("Location: themes.php?page=functions.php&reset=true");
die;
}
}
//add_menu_page(get_bloginfo('name') . " Options", "Theme Settings", 'administrator', basename(__FILE__), 'mytheme_admin', '', '63.3');
add_submenu_page('themes.php', get_bloginfo('name') . " Options", 'Theme Settings', 'administrator', basename(__FILE__), 'mytheme_admin');
}
function mytheme_admin() {
global $shortname, $options;
if($_REQUEST['saved']) echo '<div id="message" class="updated fade"><p><strong>'.get_bloginfo('name').' settings saved.</strong></p></div>';
if($_REQUEST['reset']) echo '<div id="message" class="updated fade"><p><strong>'.get_bloginfo('name').' settings reset.</strong></p></div>';
?>
<div class="wrap">
<h2><?php echo get_bloginfo('name'); ?> Settings</h2>
<form method="post" style="background-color:#f8f8f8; padding:10px; margin-top: 10px;">
<?php foreach ($options as $value) {
switch ( $value['type'] ) {
case "open":
?>
<table width="100%" border="0">
<?php break;
case "close":
?>
</table>
<?php break;
case 'text':
?>
<tr>
<td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
<td width="80%"><input style="width:400px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings($value['id'])); } else { echo stripslashes($value['std']); } ?>" /></td>
</tr>
<tr>
<td><small><?php echo $value['desc']; ?></small></td>
</tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;"> </td></tr><tr><td colspan="2"> </td></tr>
<?php
break;
case 'heading':
?>
<tr>
<td width="100%" colspan="2" valign="middle"><h3><?php echo $value['name']; ?></h3></td>
</tr>
<?php
break;
case 'textarea':
?>
<tr>
<td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
<td width="80%"><textarea name="<?php echo $value['id']; ?>" style="width:400px; height:200px;" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings($value['id'])); } else { echo stripslashes($value['std']); } ?></textarea></td>
</tr>
<tr>
<td><small><?php echo $value['desc']; ?></small></td>
</tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;"> </td></tr><tr><td colspan="2"> </td></tr>
<?php break;
}
}
?>
<input name="save" type="submit" class="button-primary" value="Save changes" />
<input type="hidden" name="action" value="save" />
</form>
<?php
}
add_action('admin_menu', 'mytheme_add_admin');
以下是从自定义设置中获取设置值的方法:
<?php
global $options;
foreach ($options as $value) {
if(get_settings($value['id']) === FALSE) { $$value['id'] = stripslashes($value['std']); } else { $$value['id'] = stripslashes(get_settings($value['id'])); }
}
?>
你把它放在你需要获取设置的文件的顶部。变量将与您在上面定义的“ID”相同。如果 ID 为“ facebook ”,则 PHP 变量将为$facebook。