How to add a new menu/submenu page in WPMVC?

I’m completely new to WPMVC. Followed and finished their tutorial for creating plugins. Now I’m trying to add a search page to search Venues (that comes from their example!). This page should appear as a submenu page of the Venues menu. I created a file named search.php in the app/views/admin/venue/search folder. What should I do now? Googling didn’t help much!

Any help regarding this (and any other stuff a noob should learn to get better with WPMVC) is welcome!

Related posts

Leave a Reply

2 comments

  1. Finally after a lot of digging into the core files, I found the way to add a submenu page. Here’s the stepwise solution if anyone is stuck on it!

    Lets assume you want to add a submenu page called ‘Sample’.

    1.First, in the app/config/bootstrap.php file (you have to manually create it if it doesn’t exist) of your plugin, add the following code. This will add the new page as a submenu of the venues parent menu item:

    <?php
    //sample is appended to the venues array, meaning it will be a submenu page of venues
    MvcConfiguration::append(array(
    'AdminPages' => array(
        'venues' => array(
            'add',
            'delete',
            'edit',
            'sample'
            )
        )
    ));
    ?>
    

    2.Next, in the app/controllers/admin/admin_venues_controller.php add the following function. Notice it is named sample, same as the page name in the array above.

    function sample() {
        //this array will be available as $values in the sample.php file
        $vals = array('great' => 'Done', 'nice' => 'Finally');
        $this->set('values', $vals);
    }
    

    3.Now the actual page content file, create a sample.php (notice the name) file in the app/views/admin/venues/ folder.

    <?php
        echo '<h2>'.MvcInflector::titleize($this->action).'</h2>';
        //remember the values variable set in the controller? 
        foreach($values as $i => $val){ ?>
            echo $i.' = '.$val;
        }
    ?>
    

    That’s pretty much it! I’m still stuck on creating a top level menu page without having to create a model (models get top level pages by default). Will update this answer when I solve it!

    Note: Emailing Tom Benner, the creator of WPMVC didn’t help too! 😉

  2. Add an action hook in your plugin main file plugin_name.php, giving the details of the menu. You can have an admin menu, of course, without generating a model.

    Here is the code for /plugins/my-plugin/my_plugin.php

    add_action( 'admin_menu', 'my_custom_menu_page' );    
    function my_custom_menu_page() {
        add_menu_page( 
            'My Menu', 
            'My Menu', 
            'manage_options', 
            'myplugin/myplugin-admin.php', 
            '', 
            plugins_url( 'myplugin/images/icon.png' ), 
            6 
        );
    }
    

    You can leave the image location empty to use a default WordPress icon for the custom menu.

    Of course, you can add submenus in the same way, or using bootstrap.php as well.

    In the picture below, I have added ‘My Menu’ by this method.

    enter image description here