How to have more than one “Theme Options” page wit

2019-05-26 02:07发布

问题:

OptionTree (GitHub) allows to create a "Theme Options" page for themes very simply.

How could I extend OptionTree in order to create a "Plugin Options" page for my plugin?

Thank you!

回答1:

It's actually pretty simple. The following code will create a page under the settings page called Test Page. This is how OptionTree creates its own pages.

/**
 * Hook to register admin pages 
 */
add_action( 'init', 'register_options_pages' );

/**
 * Registers all the required admin pages.
 */
function register_options_pages() {

  // Only execute in admin & if OT is installed
  if ( is_admin() && function_exists( 'ot_register_settings' ) ) {

    // Register the pages
    ot_register_settings( 
      array(
        array( 
          'id'              => 'custom_options',
          'pages'           => array(
            array(
              'id'              => 'test_page',
              'parent_slug'     => 'options-general.php',
              'page_title'      => 'Test Page',
              'menu_title'      => 'Test Page',
              'capability'      => 'edit_theme_options',
              'menu_slug'       => 'test-page',
              'icon_url'        => null,
              'position'        => null,
              'updated_message' => 'Test Page updated.',
              'reset_message'   => 'Test Page reset.',
              'button_text'     => 'Save Changes',
              'show_buttons'    => true,
              'screen_icon'     => 'options-general',
              'contextual_help' => null,
              'sections'        => array(
                array(
                  'id'          => 'test_section',
                  'title'       => __( 'Test Section', 'motif-core' )
                )
              ),
              'settings'        => array(
                array(
                  'id'          => 'test_section_input',
                  'label'       => 'Test Input',
                  'desc'        => 'Pretty freaking awesome!',
                  'std'         => '',
                  'type'        => 'text',
                  'section'     => 'test_section',
                  'class'       => ''
                )
              )
            )
          )
        )
      )
    );

  }

}