wordpress

wordpress Admin menu example template


Add menu,sub menu,top level menu of our plug in on admin panel of word press

Here we learn how to add the menu to word press admin side panels to control our plug ins functionality like settings ,over view etc.

Download Demo

Word press comes with some admin menus by default ,listed below

  1. Dash board– To get the over view of blog on the admin home page.
  2. Posts – mainain the blog posts
  3. Media – manage media files on blog
  4. Links-blogs default links
  5. Pages-manage blog pages
  6. Comments-manage blog comments
  7. Appearence -maintain blog themes and blog theme settings.
  8. Plugins – manage blog plug ins
  9. Users– manage blog users for admins and registered users
  10. Tools-manage database and files back up.
  11. Settings – blogs over all settings

To continue this tutorial we must aware about

  1. word press hooks ,filters and their functions.
  2. PHP OOP’s concepts

Okay, no problem even you don’t know much about word press hooks,we can manage with a main action hook for this tutorial.

admin_menu action

This action was triggered before admin menu begins and here we can add our menus .

Let start over view of the our plugin

We define the class for our plugin and instantiate it.
In the constructor our class we attach the admin_menu hook as below,that runs our class methodmy_admin_menu

add_action('admin_menu', array(&$this, 'my_admin_menu'));

Add our own panel with add_menu_page


add_menu_page(‘Panel Title’, ‘Panel first sub link title ‘, ‘level of handler ‘,’unique identifier for page’,’function that to be called’));

In the above line add_menu_page adds the main panel on the word press admin page.

add_menu_page('Mahesh Chari', 'Mahesh Chari', 'administrator', 8, 
array(&$this,'overview'));

Adding our sub links to own panel

Now we add sub links above panel titled settings, general with unique identifiers.

add_submenu_page(8, 'Settings', 'Settings', 'administrator', 1,
 array(&$this,'settings'));
add_submenu_page(8, 'General', 'General', 'administrator', 2, 
array(&$this,'generel'));

Now we add our plug ins link to existing word press panels mentioned above

Word press has already pre built functions to add respective panels.

Add sub menu link to existing posts panel

add_posts_page('Mahesh posts', 'Mahesh  Plugin', 'administrator', basename(__file__),
array(&$this, 'my_plugin_posts'));

Add sub menu link to existing Media panel

add_media_page('Mahesh media', 'Mahesh  Plugin', 'administrator', basename(__file__),
array(&$this, 'my_plugin_media'));

Add sub menu link to existing pages panel

add_pages_page('Mahesh pages', 'Mahesh  Plugin', 'administrator', basename(__file__),
array(&$this, 'my_plugin_pages'));

Add sub menu link to existing appearance panel

 add_theme_page('maheshchari', 'Mahesh  Plugin', 'administrator', basename(__file__),
array(&$this, 'my_plugin_themes'));

Add sub menu link to existing users panel

add_users_page('Mahesh users', 'Mahesh  Plugin', 'administrator', basename(__file__),
array(&$this, 'my_plugin_users'));

Add sub menu link to existing tools panel

add_management_page('maheshchari', 'Mahesh  Plugin', 'administrator',
basename(__file__),array(&$this, 'my_plugin_tools'));

Add sub menu link to existing options panel

add_options_page('Mahesh Options', 'Mahesh  Plugin', 'administrator',
 basename(__file__),array(&$this, 'my_plugin_options'));

Together all the sub menus

<?php
/*
Plugin Name: Mahesh Chari
Plugin URI: http://www.maheshchari.com/
Description: Description of my plugin
Author URI: http://www.maheshchari.com/
*/

class Maheshchari
{

    function Maheshchari()
    {
        add_action('admin_menu', array(&$this, 'my_admin_menu'));
    }

    function my_admin_menu()
    {	//create a main admin panel
		//create a sub admin panel link above
        add_menu_page('Mahesh Chari', 'Mahesh Chari', 'administrator', 8, array(&$this,'overview'));
        add_submenu_page(8, 'Settings', 'Settingss', 'administrator', 1, array(&$this,'settings'));
        add_submenu_page(8, 'Generel', 'Generel', 'administrator', 2, array(&$this,'generel'));
		//These functions adds sub menu for different kinds of admin panel on back end
        add_options_page('Mahesh Options', 'Mahesh  Plugin', 'administrator', basename(__file__),array(&$this, 'my_plugin_options'));
        add_posts_page('Mahesh posts', 'Mahesh  Plugin', 'administrator', basename(__file__),array(&$this, 'my_plugin_posts'));
        add_media_page('Mahesh media', 'Mahesh  Plugin', 'administrator', basename(__file__),array(&$this, 'my_plugin_media'));
        add_pages_page('Mahesh pages', 'Mahesh  Plugin', 'administrator', basename(__file__),array(&$this, 'my_plugin_pages'));
        add_users_page('Mahesh users', 'Mahesh  Plugin', 'administrator', basename(__file__),array(&$this, 'my_plugin_users'));
        add_management_page('maheshchari', 'Mahesh  Plugin', 'administrator', basename(__file__),array(&$this, 'my_plugin_tools'));
        add_theme_page('maheshchari', 'Mahesh  Plugin', 'administrator', basename(__file__),array(&$this, 'my_plugin_themes'));

    }

    function overview()
    {
        echo '<h2>My WordPress Plugin Overview</h2>';
    }

    function settings()
    {
        echo '<h2>My WordPress Plugin Settings</h2>';
    }

    function generel()
    {
        echo '<h2>My WordPress Plugin Generel</h2>';
    }
    function my_plugin_options()
    {
        echo '<h2>My WordPress Plugin Options</h2>';

    }
    function my_plugin_posts()
    {
        echo '<h2>My WordPress Plugin posts</h2>';
    }
    function my_plugin_media()
    {
        echo '<h2>My WordPress Plugin media</h2>';
    }
    function my_plugin_pages()
    {
        echo '<h2>My WordPress Plugin pages</h2>';
    }
    function my_plugin_users()
    {
        echo '<h2>My WordPress Plugin users</h2>';
    }

    function my_plugin_tools()
    {
        echo '<h2>My WordPress Plugin tools</h2>';
    }

    function my_plugin_themes()
    {
        echo '<h2>My WordPress Plugin themes</h2>';
    }

}

$mybackuper = &new Maheshchari();//instance of the plugin class

?>

To see the above code inaction,please download the zip of demo plug in and copy to your word press plug ins directory and activate the plugin ,then you see all the sub menus in admin panels menu

Advertisements

3 thoughts on “wordpress Admin menu example template”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s