How do you add a WordPress admin page without adding it to the menu?
📝 Blog Post: How to Add a WordPress Admin Page Without Adding it to the Menu 🚀
Are you a WordPress plugin developer looking to create a hidden admin page that is not visible in the menu? 🕵️♂️ We've got you covered! In this guide, we'll show you how to associate an admin page with a function without adding it as a menu item. Let's dive in! 💪
The Challenge: Hidden Admin Pages 🤫
The default behavior of WordPress is to add all admin pages, created using the add_menu_page()
or add_submenu_page()
functions, to the menu. However, there are instances where you may want to have an admin page that is not easily accessible through the menu. 👀
One common use case is when building a plugin with an edit-item page that shouldn't be reached via the submenu since the item wouldn't be specified. So, how do we accomplish this? Let's find out!
Solution 1: Utilizing the admin_menu
Action Hook ⚙️
The admin_menu
action hook is your secret weapon when it comes to creating hidden admin pages. By using this hook, you can register your custom page callback function without adding it to the menu. Here's an example:
function my_custom_admin_page() {
// Your custom admin page code goes here
}
function add_custom_admin_page() {
add_menu_page(
'My Custom Admin Page',
'My Custom Admin Page',
'manage_options',
'my-custom-admin-page',
'my_custom_admin_page',
'',
6 // Set a position high enough to be out of sight
);
}
add_action( 'admin_menu', 'add_custom_admin_page' );
In this example, we first define the my_custom_admin_page()
function to hold our custom admin page code. Then, we create the add_custom_admin_page()
function, which uses the add_menu_page()
function (you can also use add_submenu_page()
if needed) to attach our custom page callback function to the admin menu. Note the empty string for the $menu_icon
parameter and the high position value (6) to keep it out of sight.
By using the admin_menu
action hook, we effectively add our custom admin page without displaying it in the menu. 🎉
Solution 2: Tap into the global $submenu
Variable 🌐
Another approach to achieving a hidden admin page is by manipulating the global $submenu
variable, which holds data about the admin menu items and submenus. Here's an example:
function my_custom_admin_page() {
// Your custom admin page code goes here
}
function hide_custom_admin_page() {
global $submenu;
// Specify the parent menu slug and the item slug to remove
unset( $submenu['my-custom-menu']['my-custom-admin-page'] );
}
add_action( 'admin_menu', 'my_custom_admin_page' );
add_action( 'admin_head', 'hide_custom_admin_page' );
In this example, we define the my_custom_admin_page()
function to hold our custom admin page code. Then, we create the hide_custom_admin_page()
function, where we access the global $submenu
variable and remove our custom admin page from the submenu.
By combining the power of the admin_menu
and admin_head
action hooks and manipulating the $submenu
variable, we can effectively hide our custom admin page.
Conclusion: Conceal Your Admin Pages Like a Pro! 🎩
Whether you're a plugin developer or just a curious WordPress enthusiast, having the ability to add an admin page without adding it to the menu can be a handy trick. We've covered two solutions: utilizing the admin_menu
action hook and tapping into the global $submenu
variable.
Now it's your turn to experiment! Implement one of these solutions based on your requirements and enjoy the thrill of creating hidden admin pages. 😎
Have you ever needed to add a hidden admin page in WordPress? Share your experiences and thoughts with us in the comments below! And don't forget to subscribe to our newsletter for more WordPress tips and tricks! 💌
Happy WordPressing! ✨