2010-07-02 53 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正确地,消息将自动出现。当然,另一种选择是有条件地添加消息。即,检查表单是否已提交,如果是,则在页面标题后面的表单开始处回显消息。