2010-07-02 36 views
0

我到學習有關創建WP插件,所以我閱讀此頁 - http://codex.wordpress.org/Creating_Options_Pages創建WP插上

我嘗試在頁面這是下面的代碼給出的示例:

<?php 
// create custom plugin settings menu 
add_action('admin_menu', 'baw_create_menu'); 

function baw_create_menu() { 

    //create new top-level menu 
    add_menu_page('BAW Plugin Settings', 'BAW Settings', 'administrator', __FILE__, 'baw_settings_page',plugins_url('/images/icon.png', __FILE__)); 

    //call register settings function 
    add_action('admin_init', 'register_mysettings'); 
} 


function register_mysettings() { 
    //register our settings 
    register_setting('baw-settings-group', 'new_option_name'); 
    register_setting('baw-settings-group', 'some_other_option'); 
    register_setting('baw-settings-group', 'option_etc'); 
} 

function baw_settings_page() { 
?> 
<div class="wrap"> 
<h2>Your Plugin Name</h2> 

<form method="post" action="options.php"> 
    <?php settings_fields('baw-settings-group'); ?> 
    <table class="form-table"> 
     <tr valign="top"> 
     <th scope="row">New Option Name</th> 
     <td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td> 
     </tr> 

     <tr valign="top"> 
     <th scope="row">Some Other Option</th> 
     <td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td> 
     </tr> 

     <tr valign="top"> 
     <th scope="row">Options, Etc.</th> 
     <td><input type="text" name="option_etc" value="<?php echo get_option('option_etc'); ?>" /></td> 
     </tr> 
    </table> 

    <p class="submit"> 
    <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> 
    </p> 

</form> 
</div> 
<?php } ?> 

一切都很好,但當我在WP管理員測試窗體時,當我在窗體中插入數據並單擊更新按鈕時,沒有「更新消息」出現。

所以我的問題是當人們在表單中插入數據並點擊提交按鈕時,如何使'更新消息'或'錯誤消息'出現。

非常感謝您的幫助!

回答

1

我對此不太確定,但我建議您將add_action呼叫註冊到baw_create_menu功能之外,以便在管理員菜單之前對其進行設置。我認爲admin_init會在admin_menu之前觸發,因此您的register_mysettings函數未被調用。但我不確定這一點。

另外,我建議的WordPress的設置API閱讀以下資源:

http://codex.wordpress.org/Settings_API

http://www.presscoders.com/wordpress-settings-api-explained/

http://ottodestruct.com/blog/2009/wordpress-settings-api-tutorial/

http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/

如果使用設置API正確地,消息將自動出現。當然,另一種選擇是有條件地添加消息。即,檢查表單是否已提交,如果是,則在頁面標題後面的表單開始處回顯消息。