• I have been searching for a way to list the submenu pages of a top level admin menu added through a plugin. I couldn’t find a way to do so. However, I stumbled across a similar function in themes.php. I stripped it down a little bit to make it apply to less scenarios than those that themes.php apparently has to handle.

    This is it:

    function list_submenu_admin_pages($parent){
        global $submenu;
        if ( is_array( $submenu ) && isset( $submenu[$parent] ) ) {
            foreach ( (array) $submenu[$parent] as $item) {
                if ( $parent == $item[2] || $parent == $item[2] )
                    continue;
                // 0 = name, 1 = capability, 2 = file
                if ( current_user_can($item[1]) ) {
                    $menu_file = $item[2];
                    if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
                        $menu_file = substr( $menu_file, 0, $pos );
                    if ( file_exists( ABSPATH . "wp-admin/$menu_file" ) ) {
                        $options[] = "<a href='{$item[2]}'$class>{$item[0]}
                                      </a>";
                    } else {
                        $options[] = "<a href='admin.php?page={$item[2]}'>
                                      {$item[0]}</a>";
                    }
                }
            }
            return $options;
        }
    }

    Usage is just by passing the parent slug (the one added in the add_menu_page function).

    If there is not a function like this in WP, should I submit this for review to be included in WP? Or am I wasting my time?

  • The topic ‘List of Admin Submenu Pages’ is closed to new replies.