php - How to make multiple admin pages for one plugin?

I am trying to implement a simple login inside my plugin following this tutorial. I have successfully implemented the lo

I am trying to implement a simple login inside my plugin following this tutorial. I have successfully implemented the login inside a WP admin page, but I can't seem to redirect it to index.php. Also, index.php must still be contained inside an admin page. Here's a portion of my code:

session_start();
add_action('admin_menu', 'sample_setup_menu');

function sample_setup_menu(){
    add_menu_page('sample Foo', 'sample Foo', 'manage_options', 'sample-plugin', 'login_init');
} 

function login_init(){
    /* Check Login form submitted */    


    //$_POST['Submit']
    if(isset($_POST['Submit'])){
        /* Define username and associated password array */
        $logins = array('Alex' => '123456','username1' => 'password1','username2' => 'password2');

        /* Check and assign submitted Username and Password to new variable */
        //$Username = isset($_POST['Username']) ? $_POST['Username'] : '';
        $Username = isset($_GET['page']) ? $_GET['page'] : '';
        $Password = isset($_POST['Password']) ? $_POST['Password'] : '';

        /* Check Username and Password existence in defined array */        
        if (isset($logins[$Username]) && $logins[$Username] == $Password){
            $_SESSION['UserData']['Username']=$logins[$Username];
            header("location: index.php");
            exit;
        } else {
            /*Unsuccessful attempt: Set error message */
            $msg="<span style='color:red'>Invalid Login Details</span>";
        }
    }
    require_once('login.php');
}

I am trying to implement a simple login inside my plugin following this tutorial. I have successfully implemented the login inside a WP admin page, but I can't seem to redirect it to index.php. Also, index.php must still be contained inside an admin page. Here's a portion of my code:

session_start();
add_action('admin_menu', 'sample_setup_menu');

function sample_setup_menu(){
    add_menu_page('sample Foo', 'sample Foo', 'manage_options', 'sample-plugin', 'login_init');
} 

function login_init(){
    /* Check Login form submitted */    


    //$_POST['Submit']
    if(isset($_POST['Submit'])){
        /* Define username and associated password array */
        $logins = array('Alex' => '123456','username1' => 'password1','username2' => 'password2');

        /* Check and assign submitted Username and Password to new variable */
        //$Username = isset($_POST['Username']) ? $_POST['Username'] : '';
        $Username = isset($_GET['page']) ? $_GET['page'] : '';
        $Password = isset($_POST['Password']) ? $_POST['Password'] : '';

        /* Check Username and Password existence in defined array */        
        if (isset($logins[$Username]) && $logins[$Username] == $Password){
            $_SESSION['UserData']['Username']=$logins[$Username];
            header("location: index.php");
            exit;
        } else {
            /*Unsuccessful attempt: Set error message */
            $msg="<span style='color:red'>Invalid Login Details</span>";
        }
    }
    require_once('login.php');
}
Share Improve this question asked Jun 21, 2017 at 4:02 Bargain23Bargain23 1311 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

Using add_submenu_page function WordPress you can add multiple pages/menu

In order to add a new top-level menu to WordPress administration dashboard, You can use add_menu_page() function. This function has the following syntax.

//add plugin menu

     add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position);

As you can see, The function accepts the following parameters.

page_title: The page title.

menu_title: The menu title displayed on the dashboard.

capability: the Minimum capability to view the menu.

menu_slug: Unique name used as a slug for the menu item.

function: A callback function used to display page content.

icon_url: URL to a custom image used as the icon.

position: Location in the menu order.

Adding a Submenu

There are two types of submenus, menu items listed below your top-level menu and menu item listed below existing default menus in WordPress. To add submenus under your top-level menu, You can use add_submenu_page() function. This function has the following syntax.

//add submenu
add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function);

As you can see, this function accepts the following parameters.

parent_slug: Slug of the parent menu item.

page_title: The page title.

menu_title: The submenu title displayed on the dashboard.

capability: the Minimum capability to view the submenu.

menu_slug: Unique name used as a slug for submenu item.

function: A callback function used to display page content.

Ex:

add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
    add_menu_page('My Page Title', 'My Menu Title', 'manage_options', 'my-menu', 'my_menu_output' );
    add_submenu_page('my-menu', 'Submenu Page Title', 'Whatever You Want', 'manage_options', 'my-menu' );
    add_submenu_page('my-menu', 'Submenu Page Title2', 'Whatever You Want2', 'manage_options', 'my-menu2' );
}

try adding submenu pages

add_submenu_page( 'sample-plugin', 'Sample Page', 'title', 'administrator', 'slug-fo-page', 'slug-for-page' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745305989a4621711.html

相关推荐

  • php - How to make multiple admin pages for one plugin?

    I am trying to implement a simple login inside my plugin following this tutorial. I have successfully implemented the lo

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信