0

I am trying to create a dynamic way of adding admin pages and submenus with different capabilities.

So the first way i thought of which works BTW but is ugly is having all the pages in 1 folder and name them something like 0.manage_options.pagename.php

Then get all the pages and simply run through the array and use explode() on the name

like so:

$classLib = glob(SERVICES.'/*.php');
$pagesArray = array();
$mainMenu = '';
foreach($classLib as $className) {
    $tempPages = explode('.',str_replace('.php', '', basename($className)));
    /* a function to get nice clean name */
    $menu_title = $this->get_admin_menu_clear_name($tempPages['2']);

    $pagesArrayTemp[$tempPages[0]] = array(
        'page_title' => $tempPages['2'],
        'menu_title' => $menu_title,
        'capability' => $tempPages['1'],
        'menu_slug' => $tempPages['2'],
        'url' => 'pluginame/services/'.basename($className),
    );
}        
$pagesArray = array();
foreach($pagesArrayTemp as $tempPage){
    $pagesArray[] = $tempPage;
}
add_menu_page('pluginame', 'Main', $pagesArray[0]['capability'], $pagesArray[0]['url']);

foreach($pagesArray as $page){
    add_submenu_page('pluginame', $page['page_title'], $page['menu_title'], $page['capability'], $page['url']);
}

this works however the URI of the page are ridiculously ugly.

So i am trying to find an alternative way. without creating a function for each page ....

is there a way to pass a parameter to add_menu_page or add_submenu_page function ?

like:

add_submenu_page('something', 'something', 'something' , 'manage_options', 'something', array($this, 'option'));

function option($premission, $pageURL){
    if ( !current_user_can( $premission ) )  {
    wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
    require_once($pageURL);
}

maybe a filter or something alike ?

Thanks in advanced.

  • Please share your solution as an answer so others can benefit. – Dave Romsey Nov 4 '13 at 21:32
  • its not a clean solution but i will – Neta Meta Nov 4 '13 at 21:40
1

What i ended up doing is setting the page array as a class variable then doing the normal add add_menu_page or add_submenu_page.. while the callback function is the same function.

in callback function i take the $_GET['page'] and then look in the array i put inside the class's variable..

consider that page's name are: 0.manage_options.pagename.php

like so:

$classLib = glob(SERVICES.'/*.php');
$pagesArray = array();
$mainMenu = '';
 foreach($classLib as $className) {
    $tempPages = explode('.',str_replace('.php', '', basename($className)));
    $menu_title = $this->wp_utils->get_admin_menu_clear_name($tempPages['2']);

    $pagesArrayTemp[$tempPages[0]] = array(
        'page_title' => $tempPages['2'],
        'menu_title' => $menu_title,
        'capability' => $tempPages['1'],
        'menu_slug' => $tempPages['2'],
        'name' => basename($className),
    );
 }

$pagesArray = array();
foreach($pagesArrayTemp as $tempPage){
    $pagesArray[] = $tempPage;
}
$this->pages = $pagesArray;
add_menu_page(PLUGIN_NAME, PLUGIN_NAME, $pagesArray[0]['capability'], $pagesArray[0]['menu_slug'], array($this, 'menu_pages'));
foreach($pagesArray as $page){
    add_submenu_page($pagesArray[0]['menu_slug'], $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], array($this, 'menu_pages'));
}


protected function menu_pages() {
    foreach($this->pages as $page){
        if($page['menu_slug'] == $_GET['page']){
            $capability = $page['capability'];
            $pageName = $page['name'];
        }
    }

    if ( empty($capability) || !current_user_can( $capability ) )  {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }
    require_once(dirname(__FILE__).'/services/'.$pageName);
}

I know this is not a clean way but since i did not find any other solution this is what i came up with.

Hopes that helps someone.

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.