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