2

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
        );
  • 1
    Because no one implemented it? Open a Trac ticket with your feature request. – fuxia Oct 9 '12 at 15:55
  • 1
    NOooooooooo kidden! I sure will! – Ben Racicot Oct 9 '12 at 15:58
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.

0

Simply put - because callback is sufficient to do anything, including loading file. :) It's not worth complicating logic for something that is already not only possible but trivial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.