Created
April 28, 2018 02:49
-
-
Save ahmedmusawir/5b02a41dde1f3a0d99d84fb44ccd1f01 to your computer and use it in GitHub Desktop.
PLUGIN-DEV - TOP LEVEL ADMIN MENU WITH SUB MENUS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function page_callback_function() { | |
// check if user is allowed access | |
if ( ! current_user_can( 'manage_options' ) ) return; | |
?> | |
<div class="wrap"> | |
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1> | |
<form action="options.php" method="post"> | |
<?php | |
// output security fields | |
settings_fields( 'myplugin_options' ); | |
// output setting sections | |
do_settings_sections( 'myplugin' ); | |
// submit button | |
submit_button(); | |
?> | |
</form> | |
</div> | |
<?php | |
} | |
function page_callback_function1() { | |
// check if user is allowed access | |
if ( ! current_user_can( 'manage_options' ) ) return; | |
?> | |
<div class="wrap"> | |
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1> | |
<form action="options.php" method="post"> | |
<?php | |
// output security fields | |
settings_fields( 'myplugin_options' ); | |
// output setting sections | |
do_settings_sections( 'myplugin' ); | |
// submit button | |
submit_button(); | |
?> | |
</form> | |
</div> | |
<?php | |
} | |
function page_callback_function2() { | |
// check if user is allowed access | |
if ( ! current_user_can( 'manage_options' ) ) return; | |
?> | |
<div class="wrap"> | |
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1> | |
<form action="options.php" method="post"> | |
<?php | |
// output security fields | |
settings_fields( 'myplugin_options' ); | |
// output setting sections | |
do_settings_sections( 'myplugin' ); | |
// submit button | |
submit_button(); | |
?> | |
</form> | |
</div> | |
<?php | |
} | |
// add top-level administrative menu | |
function myplugin_add_toplevel_menu() { | |
add_menu_page( 'Page Title', 'Menu Title', 'manage_options', 'menu_slug', 'page_callback_function', 'dashicons-media-spreadsheet' ); | |
add_submenu_page( 'menu_slug', 'Page Title 1', 'Sub-menu 1 Title', 'manage_options', 'submenu1_slug', 'page_callback_function1' ); | |
add_submenu_page( 'menu_slug', 'Page Title 2', 'Sub-menu 2 Title', 'manage_options', 'submenu2_slug', 'page_callback_function2' ); | |
} | |
add_action( 'admin_menu', 'myplugin_add_toplevel_menu' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment