0

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');
}
1

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' );
}
0

try adding submenu pages

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

protected by Community Jul 22 at 11:09

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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