Tutorial

How to add a page to the WordPress admin sidebar menu

This is a brief guide on how to add a custom top-level menu item and corresponding admin screen to the WordPress admin sidebar.

By Code CobaltJanuary 1, 2024
How to add a page to the WordPress admin sidebar menu

The code in this tutorial can be added to your theme's functions.php, added using a code snippets plugins like Code Snippets, or elsewhere in a custom plugin or child theme.

Step 1: Add admin menu hook

First, let's add an action for the admin_menu hook.

php

Let's take a look at the options we have available in add_menu_page. Here is the function prototype:

php

1. page_title – Title of the page used in the title tags. 2. menu_title – Text to be displayed in the sidebar. 3. capability – The capability required for this menu item to be displayed. "manage_options" is a good choice to restrict access to Administrators. Read more at WordPress Roles and Capabilities 4. callback – The function that will output the content for the page. 5. icon_url – Optional icon for the class. You can use icons here WordPress Dashicons 6. position – A priority number to control the relative position of the element in the sidebar. Read more here: WordPress add_menu_page documentation

Step 2: Output the contents of the page

Define the function in your callback from Step 1. Use any method to output the necessary HTML.

php

And we are done! You should now see your menu on the sidebar and your content should display when you select it. Custom WordPress admin page sidebar

Full Example

php

Ready to get started?

Let's discuss how we can help bring your digital vision to life. Get a free consultation or request a custom quote.

Or email us at contact@codecobalt.com