Wordpress drill-down admin pages II
January 18, 2010
I never did do a follow-up on the Wordpress drill-down admin menus post. A lot of people continue to hit that article so I’ll take the time to give an example of how I easily resolved the issue.
The problem was that after an upgrade to Wordpress Mu v 2.8.4a all my dynamically created, drill-down admin pages quit working because they now needed to be registered with Wordpress for — completely understandable — security reasons. As far as I can tell, the only way to register an admin page is through the menu system. Since I don’t want my drill down pages to be on the menu I needed a better way to do it.
One solution suggested was to just use the registered menu page as the callback to handle requests and that is for the most part what I did. The only issue with the solution is that the callback page has to also serve all the content since it can’t request an admin page that is not registered. Using one page to parse all requests and serve the appropriate content will get unwieldy for anything but the simplest of applications and if your application was that simple you wouldn’t be trying to figure out how to add custom drill-down admin menus.
I like modularity and easily managed code so my philosophy is that multiple small files are better than one huge file. I needed a way to solve the problem without breaking my philosophy or changing a lot of my existing code, which was already in multiple small files.
The idea is to build a container page to register on the admin menu and have the container page decide on which code to include based on analyzing the url. The included code can then handle and GET or POST requests and serve the appropriate page output.
<?php
/*
* Descrition: gcgc admin page. This is the main container page
* for all admin panels. All POSTS and GETS from all pages route
* through this page and are handled by the appropriate included file.
*/
global $gcgc_Manager;
echo $gcgc_Manager->getHeader();
//analyze the url to determine which page to load
if (isset($_GET['panel'])){
switch ($_GET['panel']){
case 'cache-admin' :
include (dirname(__FILE__)."/gcgc-admin-cache.php");
break;
case 'vector-admin' :
include (dirname(__FILE__)."/gcgc-admin-vector.php");
break;
default :
include (dirname(__FILE__)."/gcgc-admin-home.php");
}
}else{
include (dirname(__FILE__)."/gcgc-admin-home.php");
}
$gcgc_Manager->getFooter();
?>
I kept the above example brief but you can imagine that there can be an unlimited number of admin pages. And this way I didn’t have to change the included files at all — all I had to do was add the ‘&panel=’ parameter to existing request urls and problem solved.
Posted by admin