我创建了一个文件,将一个选项页面添加到WP自定义帖子类型,该类型应该显示幻灯片的选项,但是我在保存数据时遇到了问题。基本上,当您单击提交按钮时,我希望它更新该选项。谢谢你的帮助:)
这是我的代码:
{
<?php
// Add Options in the database.
add_option('slider_width', 960);
add_option('slider_height', 350);
// Create options to display.
$prefix = 'slider';
$options = array(
array(
'name' => 'Slider Width',
'desc' => 'Set the width of the slideshow in pixels.',
'id' => $prefix.'_width',
'type' => 'text'
),
array(
'name' => 'Slider Height',
'desc' => 'Set the height of the slideshow in pixels.',
'id' => $prefix.'_height',
'type' => 'text'
),
array( "type" => "close"),
array( 'type' => 'section'),
array( "type" => "open")
); // end $options
// Add options page to admin area
function slider_add_admin() {
global $prefix, $options;
if ( $_GET['page'] == 'slider_options' ) {
if ( 'save' == $_REQUEST['action'] ) {
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: edit.php?post_type=sliders&page=slider_options&saved=true");
die;
}
else if( 'reset' == $_REQUEST['action'] ) {
foreach ($options as $value) {
delete_option( $value['id'] ); }
header("Location: edit.php?post_type=sliders&page=slider_options&reset=true");
die;
}
}
// Create Subpage
add_submenu_page('edit.php?post_type=sliders','Slider Page Options','Slider Options', 'edit_sliders', 'slider_options', slider_options);
}
function slider_options(){
global $prefix, $options;
$i=0;
if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>Slider settings saved.</strong></p></div>';
if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>Slider settings reset.</strong></p></div>';
?>
<div class="wrap rm_wrap">
<h2>Slider Options</h2>
<div class="rm_opts">
<form method="post">
<?php
// Add cases for the options.
foreach ($options as $value) {
switch ( $value['type'] ) {
case 'open':
break;
case 'close':?>
</div>
</div>
<br />
<?php
break;
case 'text':
echo '<label for="'.$value['id'].'">'.$value['name'].'</label>';
echo '<input type="text" name="'.$value['name'].'" id="'.$value['id'].'" value="';
if( get_option($value['id']) != "" ){echo get_option($value['id']);}
echo '" size="30" /><small class="description">'.$field['desc'].'</small>';
break;
case 'checkbox':
echo '<label for="'.$value['id'].'">'.$field['desc'].'</label>';
echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/>';
break;
case 'section':
$i++;
echo '<div class="rm_section">';
echo '<div class="rm_title"><h3><img src="'.get_bloginfo('template_url').'/images/logo.png" class="inactive"/>';
echo $value['name'];
echo '</h3><span class="submit"><input name="save'.$i.'" type="submit" value="Save Changes" /></span>';
echo '<div class="clearfix"></div></div>';
break;
} // end switch
} //end foreach
?>
<?php echo get_option('slider_width'); ?>
<input type="hidden" name="action" value="save" />
</form>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
</div>
<?php
} // end slider_options
add_action('admin_menu', 'slider_add_admin');
?>
}
答案 0 :(得分:0)
这可能对您有所帮助;我为仪表板创建选项页面有点不同,但它允许我在表单中创建我想要的任何内容。我在functions.php中包含了这个文件。例如,它会保存单个输入字段,但您可以通过添加更多行来创建更多字段:
register_setting('example_options_settings-group','example_some_other_field');
然后是example_options_settings_page()表单中的相应输入。
<?php
add_action('admin_menu', 'example_options');
function example_options() {
$icon_path = get_bloginfo('template_url').'/images/icon-plugin.png';
add_menu_page('Options', 'Options', 'delete_others_pages', __FILE__, 'example_options_settings_page',$icon_path, 115);
add_action( 'admin_init', 'example_options_settings' );
}
function example_options_settings() {
register_setting( 'example_options_settings-group', 'example_contact_url' );
}
function example_options_settings_page() { ?>
<div class="wrap theme-options">
<div class="icon32" id="icon-options-general"><br /></div>
<h2>Theme Options</h2>
<form method="post" class="theme-options-form" action="options.php">
<?php settings_fields( 'example_options_settings-group'); ?>
<div class="postbox metabox-holder">
<h3 class="hndle">Contact Info</h3>
<div class="inside">
<div class="option-panel">
<p><label for="example_contact_url">Enter Email Address</label></p>
<p>
<?php $example_contact_url = get_option('example_contact_url'); ?>
<input id="example_contact_url" name="example_contact_url" value="<?php echo $example_contact_url; ?>" style="width: 90%;" type="text" />
</p>
</div>
</div>
</div>
<input type="hidden" value="1" />
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php } ?>