There is the codes.

 add_action('admin_menu', 'test');

 function test(){       add_menu_page( 'Test plugin', 'Test plugin',
 'manage_options', 'test_plugin_options', 'test_plugin_admin_options'
 ); }

And there is the add_meta_box code:

function inf_custome_box() {
    add_meta_box( 'inf_meta', __( 'Meta Box Title', 'test' ), 'custome_box_callback', 'test_plugin_options' );
}
add_action( 'add_meta_boxes', 'inf_custome_box' );

function custome_box_callback() {
    echo 'This is a meta box';  
}

But it’s not working, the meta_box isn’t apper.

What can i do wrong?

1 Answer
1

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 Reply

Your email address will not be published. Required fields are marked *