tags:

views:

20

answers:

1

Hello there,

I'm up to study about creating WP Plug in and so I read this page - http://codex.wordpress.org/Creating_Options_Pages

I try the example given in that page which is the below code :

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

Everything is just fine but when I test the form in the WP admin, when I insert the data in the form and click update button, there is no 'update message' appear.

So my question is how to make 'update message' or 'error message' appear when people insert data in the form and click the submit button.

Thanks a lot for your help!

+1  A: 

I'm not sure about this, but I'd suggest taking the add_action call for registering your settings out of the baw_create_menu function so that it's set up before admin menu. I think admin_init fires before admin_menu, so your register_mysettings function isn't being called. But I'm not sure about that.

Also, I'd suggest reading the following resources on the WordPress Settings 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/

If you use the Settings API correctly, the message will appear automatically. Of course, the other option is adding the message conditionally. I.e., check if the form was submitted, and if so, echo the message at the beginning of the form, just after the page's title.

John P Bloch