mytechead

Android & Wordpress Help center

WordPress : Add administration/settings menu for plugins

To add an administration menu, you must do three things:

  1. Create a function that contains the menu-building code
  2. Register the above function using the “admin_menu” action hook (if you are adding an admin menu for the Network, use “network_admin_menu” instead)
  3. Create the HTML output for the page (screen) displayed when the menu item is clicked

It is that second step that is often overlooked by new developers. You cannot simply call the menu code described; you must put it inside a function, and then register the function.

Here is a very simple example of the three steps just described. This plugin will add a sub-level menu item under the Settings top-level menu, and when selected, that menu item will cause a very basic screen to display. Note: this code should be added to a main plugin PHP file or a separate PHP include file.

<?php 
add_action( 'admin_menu', 'my_plugin_menu' ); 
function my_plugin_menu() { 

// My Plugin Options => Page title
// My Plugin => Label in sub-menu
// manage_options => capability
// my-unique-identifier => page identifier 
// my_plugin_options => call back function name

add_options_page( 'My Plugin Options', 
'My Plugin', 
'manage_options', 
'my-unique-identifier', 
'my_plugin_options' ); 


function my_plugin_options() { 
if ( !current_user_can( 'manage_options' ) ) { 
 wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); 

echo '<div>'; echo '<p>Here is where the form would go if I actually had options.</p>'; 
echo '</div>'; 

?> 

In this example, the function, my_plugin_menu(), adds a new item to the Administration menu via the add_options_page function. Note: more complicated multiple menu items can be added, but that will be described later. Notice the add_action line–that invokes the hook which “registers” the function, my_plugin_menu(). Without that add_action, a PHP error for “undefined function” will be thrown when attempting to activate the plugin. Finally, the add_options_page code refers to the my_plugin_options() function which contains the actual page to be displayed (and PHP code to be processed) when someone clicks the menu item.

The actual detail of these processes is described in more detail in the sections below. Remember to enclose creation of the menu and the html page in functions, and invoke the admin_menu hook to get the whole process started!

The above code will create a sub-menu under the Settings tab with the name “My Plugin”

References : http://codex.wordpress.org/Administration_Menus

Advertisements

Single Post Navigation

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

%d bloggers like this: