add_submenu_page callback a file instead of a function?

I want to clean up my theme options because I can’t keep track of everything. How can I get my page_callback to link to a file sintead of a callback?

I know that I could include the file in the callback but why if I can simply call the file here?

  add_submenu_page( 
              null            // -> Set to null - will hide menu link
            , 'Page Title'    // -> Page Title
            , 'Menu Title'    // -> Title that would otherwise appear in the menu
            , 'administrator' // -> Capability level
            , 'menu_handle'   // -> Still accessible via admin.php?page=menu_handle
            , 'page_callback' // -> To render the page
        );

2 Answers
2

Instead of running to trac and complaining about a missing feature, I suggest to make use of an OOP construct:

// File: base.class.php
abstract class wpse67649_admin_page_base
{
    public function add_page()
    {
        add_submenu_page( 
             null            // -> Set to null - will hide menu link
            ,'Page Title'    // -> Page Title
            ,'Menu Title'    // -> Title that would otherwise appear in the menu
            ,'administrator' // -> Capability level
            ,'menu_handle'   // -> Still accessible via admin.php?page=menu_handle
            ,array( $this, 'render_page' ) // -> To render the page
        );
    }

    // Must get defined in extending class
    abstract function render_page();
}

// File: ___sub_page.class.php
class wpse67649_render_sub_page extends wpse67649_admin_page_base
{
    public function __construct()
    {
        add_action( 'admin_init', array( $this, 'add_page' ) );
    }

    public function render_page()
    {
        // You have access to every $this->class_var from your parent class
        ?>
<div class="wrapper>
    <!-- do funky page rendering -->
</div>
        <?php
    }
}

This isn’t the final thing, but it should bring you on route to get stuff better organized.

Leave a Comment