How do you add a WordPress admin page without adding it to the menu?

Cover Image for How do you add a WordPress admin page without adding it to the menu?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 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!


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello