WordPress.org

Support

Support » Plugins and Hacks » How to point "add_menu_page" to a file rather than function

How to point "add_menu_page" to a file rather than function

  • I added a new menu item for my plugin like this:

    add_menu_page(
      'Control Panel',
      'My Plugin',
      'manage_options',
      'control-panel',
      array($this, 'control_panel_page'),
      plugins_url('/images/menu_icon.png', __FILE__)
    );
    public function control_panel_page() {
      // My HTML goes here
    }

    However, I feel uncomfortable with the fact that all my HTML resides in PHP function (“control_panel_page”).

    Is there an option/hack to point to HTML file rather than PHP function?

Viewing 1 replies (of 1 total)
  • However, I feel uncomfortable with the fact that all my HTML resides in PHP function (“control_panel_page”).

    Why are you uncomfortable with that? A huge percentage of your site’s HTML is in some PHP function or another. That is a big part of the “why” of using PHP.

    No, you cannot point that to a file. It needs to be a callback function. You can extract your code to a file and then do something like:

    control_panel_page() {
        include('control_panel_file.php');
    }

    But there is no reason to do that unless you think it makes the code easier to maintain, which it does, sometimes, with large and complex pages.

Viewing 1 replies (of 1 total)
  • The topic ‘How to point "add_menu_page" to a file rather than function’ is closed to new replies.
Skip to toolbar