Friday, August 5, 2011

Nerd Post: Hacking Wordpress's admin pages to add a hidden link

As some of you know, Kinetic Thoughts is working in partnership with another firm to produce some niche Wordpress plugins. In creating the plugins, we wanted to create an admin menu, with sub-menu entries. No problem. Wordpress has mechanisms for that.

Now, when you add in a menu choice (or sub-menu choice) the URL generated when the menu is click is something akin to: http://host/wp-admin/admin.php?page=your_custom_menu_handle

This handle is actually created with the code like (assuming it is a sub-menu, the menu's function has a slightly different signature):

add_submenu_page(
'my_plugins_toplevel_link'
, 'Page Title (seen in browser)'
, 'Menu Link Text'
, 'administrator' //security level
, 'your_custom_menu_handle' // here's the page handle for page one
, 'page_callback_function' //the function that renders this menu's page
);



Now, I want to get to my sub-menu page and preserve the URL structure of:
http://host/wp-admin/admin.php?page=your_custom_menu_handle
One way to do this is to add a sub-menu entry pointing to your first sub-menu's handle:
add_submenu_page(
'your_custom_menu_handle' // set the parent to your first page and it wont appear
, 'Hidden Menu Page Title (seen in brower)'
, 'Hidden Menu Link Name' // unused
, 'administrator' //security level
, 'your_hidden_menu_custom_handle'
, 'hidden_page_callback_function' //the function that renders this menu's page
);



I want to thank the guys on this post at StackOverFlow.


This works quite well. Now I don't condone hacks usually, but sometimes you just got to get to the MVP (Minimum Viable Product) and fix things later. More than likely, Wordpress was coded to allow for multiple levels of sub-menus, but only displays the first. Something to investigate in the future.

Nice, huh?

Jeff

No comments: