Need a WordPress website this weekend? Start here...

DIY WordPress: Really Simple Plugin Admin Menus

(Reading time: 3 – 4 minutes)

Following our Technical Tuesday theme, here’s another stupendously popular WordPress Plugin mini-tutorial.

This tutorial shows how to create menu and submenu items in your left sidebar in the WordPress administration interface.

Here’s what it looks like:
Admin Menu WordPress Administration page

The blue arrow points to a spiffy menu icon, the orange highlighting to the menu title, and the green highlighting to the submenus. The name of the plugin is “Demo Admin Menu.”

First, let’s examine the structure of the the plugin.

WordPress Plugin Structure

This following listing is a pseudo-code. It looks like PHP but it’s not. It’s like PHP with all the confusing syntactical parts stripped out, and simplified functions. As you can see, all that’s left is the structure of the code, which is exactly what we want:

class demo_plugin_adminmenu {

    function demo_plugin_adminmenu() {
        add_action(admin_menu, add_demo_menu)
    }

    function add_demo_menu() {
        add_menu_page( call demo_menu_page)
        add_submenu_page( call demo_submenu_page')
    }

    function demo_menu_page() { write a web page }
    function demo_submenu_page() { write a web page }
}

Each function listed in the pseudo-code above may have one number or more arguments. The Codex documents the WordPress functions add_action, add_menu_page and add_submenu_page.

Part of the reason for this article is that the Codex doesn’t document the exact syntax for these calls, which depends on the context in which these calls are made. If the calls are made outside of a class, use the Codex version directly. If made within a class, as shown here, the callbacks will need to be wrapped as shown in the code listing next.

WordPress Admin Menu Plugin

Here’s the final code listing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
 
 
if (!class_exists("demo_plugin_adminmenu")) {
 
    class demo_plugin_adminmenu {
 
        function demo_plugin_adminmenu() {
            add_action('admin_menu', array(&$this, 'add_demo_menu'));
        }
 
 
        function add_demo_menu() {
            if (function_exists('add_menu_page')) {
 
                add_menu_page('Menu Page Title', 'Menu Title', 
				              'administrator', __FILE__, 
							  array(&$this, 'demo_menu_page'), 
							  WP_PLUGIN_URL.'/demo-plugin-adminmenu/award_star_gold_1.ico');
 
                add_submenu_page(__FILE__, 'Page Title', 'Submenu Title', 
				              'administrator', 'submenu_handle', 
							  array(&$this, 'demo_submenu_page'));
            }
        }
 
        function demo_menu_page() {    
			?>
			<div class="wrap">
			    <h2>Demo Menu Page</h2>
			    Does nothing but demonstrate menu.
			</div>
			<?php 
        }
 
        function demo_submenu_page() {
			?>
			<div class="wrap">
			    <h2>Demo Submenu Page</h2>
			    Does nothing but demonstrate submenu. 
			</div>
			<?php 
        }
    }
}
 
$wpdpd = new demo_plugin_adminmenu();
 
?>

You could copy and paste this code listing. But you would learn it better if you typed in by hand, which is what often do. Don’t forget to add the plugin header information.

WordPress Plugin Tutorials

These mini-tutorials are designed to provide a single lesson resulting in runnable code, with no extras.

Each provides a simple, standalone plugin. You can use these plugins like bricks, combining to create your own plugins.

Often, these tutorials go beyond the WordPress Codex to demonstrate new techniques which aren’t documented in the Codex.

Comments

  1. I love these technical tuesday articles. Keep em going.

    I knew about this functionality but hadn’t bothered checking it out. Just another one of those things that I knew could be done, but hadn’t figured out HOW.

    Consider this delicious’d. Yum.
    .-= Josh Kohlbach´s last blog ..StumbleUpon: My Stumble Your Own Stuff Experiment =-.

    • Dave Doolin says:

      Josh, I have a whole series of these plugins. Each taps on a different aspect of plugin functionality, showcasing only that functionality.

      Not sure the best way to distribute and promote these yet.

  2. Heather says:

    I’d been wondering how to do that; friend of mine’s just been figuring it out alone, I’ll have to see if he managed already. Having the very beginnings of an idea though, think it needs to bubble for a bit.

    Thanks Dave, you rock.
    .-= Heather´s last blog ..The Mayans: Login Part 2 =-.

  3. Can I play devil’s advocate and ask what you might use this for? It looks very snazzy and clever, but unless your admin area is shared with others, who is going to share in this cleverness? Or am I missing the point totally? I’m not decrying the clever coding, just wondering what practical use I can put this to.
    .-= Eleanor Edwards´s last blog ..#CharityTuesday: The Big Audio Challenge =-.

  4. Chris says:

    That’s what I love so much about wordpress. There are just so much addons and information available this platform. And best of all, there are even such detailed tutorials for people like me what are not very technically strong. I’m more of a logo design guy. :)

    [Edit: Pure advertisement. Is he any good? Let's find out... No. He's not. He's running Kubrik theme, with no logos at all. WTF? I'm going to leave the link up. Go visit, I don't care, see how silly it is and what a waste of everyone's time.

    Chris, put a portfolio and some logos up, I'll be more than happy to relink you. Delighted, seriously.]

    .-= Chris´s last blog ..How to run a successful logo design contest =-.

Trackbacks

  1. JournalXtra says:

    101 Incredibly Good WordPress Plugins…

    I found your entry interesting do I’ve added a Trackback to it on my weblog :)…