WordPress.org

Ready to get started?Download WordPress

Forums

How to point "add_menu_page" to a file rather than function (2 posts)

  1. moroshko
    Member
    Posted 1 year ago #

    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?

  2. s_ha_dum
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic