2012-02-22 90 views
1

我已經創建了一個文件,該選項頁面添加了一個WP自定義帖子類型,該類型應該顯示幻燈片放映選項,並且在保存數據時遇到問題。基本上,當你點擊提交按鈕,我希望它更新選項。感謝您的幫助:)Wordpress中的更新選項自定義帖子類型選項頁面

這裏是我的代碼:

{

<?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

問題可能出現在您的'header'命令中,當頁面沒有返回到自身並且沒有保存您的數據時。點擊提交時你會得到什麼?你回到你的帖子列表?另外,請嘗試在'header'命令中將'&page = slider_options'更改爲'&page = slider_options.php',看看它是否適合您。 – Shebo 2012-04-03 18:18:12

回答

0

這可能會對你有所幫助;我爲Dashboard創建選項頁面有點不同,但它允許我在表單中創建任何我想要的東西。我從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 } ?> 
相關問題