Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am attempting to create a WordPress plugin which allows the client to use for help and other resources in a friendly interface. It would be desired if I could have guidance on creating a WordPress plugin with a main dashboard menu and submenus on it which can contain HTML content. As I am a new to WordPress Development, I found it hard to understand the WP Codex.

snapshot

This is the plugin code:

/** add menu. */
add_action( 'admin_menu', 'my_plugin_menu', 'my_magic_function' );
add_submenu_page( 'my_plugin_menu', 'Page title', 'Sub-menu title', 'manage_options', 'my-submenu-handle', 'my_magic_function');


function my_plugin_menu() {
        add_options_page( 'My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options' );
}

function my_plugin_menu() {
        add_options_page( 'Submenu', 'My Plugin', 'manage_options', 'my-submenu-handle', 'my_plugin_options' );
}

/** html. */
function my_plugin_options() {
        if ( !current_user_can( 'manage_options' ) )  {
                wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
        }
        echo '<div class="wrap">';
        echo '<p>Main page</p>';
        echo '</div>';
}

function my_plugin_options() {
        if ( !current_user_can( 'manage_options' ) )  {
                wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
        }
        echo '<div class="wrap">';
        echo '<p>submenu page</p>';
        echo '</div>';
}
share|improve this question
1  
This is pretty clear codex.wordpress.org/Administration_Menus, can you show us what you have tried so far. –  Wyck Jul 8 '13 at 16:32
    
@Wyck Here is my code, where am I wrong? pastebin.com/d4x4yD3L Thanks, Sam –  samuelrogers Jul 8 '13 at 16:49
    
Please post your code into your question and add any problems you are having with it. –  Wyck Jul 8 '13 at 16:52
    
Unfortunately, I cannot post the completecode on the comment section because it overrides the character limit. WordPress does not like this line of code when attempting to register a submenu: function my_plugin_menu() { add_options_page( 'Submenu', 'My Plugin', 'manage_options', 'my-submenu-handle', 'my_plugin_options' ); } Here is the code on Pastebin: pastebin.com/LPFgxspQ –  samuelrogers Jul 8 '13 at 16:55
    
You should use add_menu_page or add_options_page, also you have several functions with the same name you cannot do that in PHP, turn on debugging codex.wordpress.org/Debugging_in_WordPress , also read the part about the callback $function on this page codex.wordpress.org/Function_Reference/add_menu_page –  Wyck Jul 8 '13 at 17:00
show 1 more comment

1 Answer

up vote 0 down vote accepted

Here is a simple example to get you started with the right code. This creates a main menu item using add_menu_page then attaches a submenu using add_submenu_page. Both of them call a different function for the output.

Notice that the add_submenu_page function ties into the parent menu using customteam which is the $menu_slug of add_menu_page.

add_action( 'admin_menu', 'register_my_custom_menu_page' );
add_action( 'admin_menu', 'register_my_custom_submenu_page' );

function register_my_custom_menu_page(){
    add_menu_page( 'Team Kit', 'Team Kit', 'manage_options', 'customteam', 'my_custom_menu_page'); 
}

function register_my_custom_submenu_page() {
    add_submenu_page( 'customteam', 'Team info', 'Team info', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page' ); 
    add_submenu_page( 'customteam', 'Crew Stats', 'Crew Stats', 'manage_options', 'my-custom-submenu-page_2', 'my_custom_submenu_page_2' );
    //add_submenu_page_3 ... and so on
}

function my_custom_menu_page() {
    echo '<p>Hello, I am Team Kit</p>';
}

function my_custom_submenu_page() {
    echo '<p>Hello, I am Team Info</p>';
}

function my_custom_submenu_page_2() {
    echo '<p>Hello, I am Crew Stats</p>';
}
share|improve this answer
    
Thanks for this, how would I add additional subpage(s) to this plugin? Sorry for my lack of knowledge, and thanks for the amazing help! –  samuelrogers Jul 8 '13 at 19:38
    
Just call more add_submenu_page's I edited my answer. –  Wyck Jul 8 '13 at 20:06
    
thank you, I have tried using the following code, but it does not display all the sub menus: pastebin.com/raw.php?i=Qj9k8XKm –  samuelrogers Jul 9 '13 at 15:23
    
You're missing add it to add_action, Ill fix my answer. –  Wyck Jul 9 '13 at 15:26
    
I have tried the new code and it says this as an error: Fatal error: Call to undefined function add_submenu_page_2() –  samuelrogers Jul 9 '13 at 15:35
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.