How to add meta box to plugin admin page?

add_meta_boxes is for adding meta boxes to post types. What you’re looking for is the Settings API. In your add_menu_page function you are calling a function named test_plugin_admin_options. This function will hold the content of your options page. You will also need to register your settings with register_setting().

//add menu page
add_action('admin_menu', 'test');
function test(){       
    add_menu_page( 'Test plugin', 'Test plugin', 'manage_options', 'test_plugin_options', 'test_plugin_admin_options' ); 
}

//register settings
add_action( 'admin_init', 'register_test_plugin_settings' );
function register_test_plugin_settings() {
    //register our settings
    register_setting( 'test-plugin-settings-group', 'new_option_name' );
    register_setting( 'test-plugin-settings-group', 'some_other_option' );
}

//create page content and options
function test_plugin_admin_options(){
?>
    <h1>Test Plugin</h1>
    <form method="post" action="options.php">
        <?php settings_fields( 'test-plugin-settings-group' ); ?>
        <?php do_settings_sections( 'test-plugin-settings-group' ); ?>
        <table class="form-table">
          <tr valign="top">
          <th scope="row">New Option 1:</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">New Option 2:</th>
          <td><input type="text" name="some_other_option" value="<?php echo get_option( 'some_other_option' ); ?>"/></td>
          </tr>
        </table>
    <?php submit_button(); ?>
    </form>

<?php } ?>

Leave a Comment