Pass $_POST data to a function

I have been working on login plugin below. What I am trying to some basic data validation. I want to check if the form fields are empty, and to display the appropriate error message. I am having difficult passing the $_POST data to validatedata function.

Is the a way to safely pass data to the validatedata function?

function loginform(){
errormsg();
return ‘
<form method=”post” action=”‘.esc_url(“http://findevsvr01:83/index.php/login/”).'”>
<fieldset>
<legend>User Login</legend>
<input type=”text” name=”username” value=”‘.(isset($_POST[“username”]) ? esc_attr($_POST[“username”]):”).'”placeholder=”Username”/>
<br />
<input type=”password” name=”password” value=”‘.(isset($_POST[“password”]) ? esc_attr($_POST[“password”]):”).'” placeholder=”Password”/>
<br />
<input type=”submit” name=”login” value=”Login”/>
</fieldset>
</form>
‘;
}

function validatedata(){
$errors = new WP_error();
if(empty($username || $password)){
$errors->add(‘required_fields’, ‘All Fields Required’);
}elseif(empty($username)){
$errors->add(‘username_required’, ‘Please Enter Username’);
}elseif(empty($password)){
$errors->add(‘password_required’, ‘Please Enter Password’);
}

return $errors;
}

function errormsg(){
global $errors;
$formErr = validatedata();

if(is_wp_error($formErr) || !empty($formErr->errors)){
foreach($formErr->get_error_messages() as $errMsg){
echo ‘<div>’. $errMsg .'</div>’;
}
}
}

function login(){
if(isset($_POST[‘login’])){
$username = sanitize_text_field($_POST[‘username’]);
$password = sanitize_text_field($_POST[‘password’]);
//pass $_POST data to validate function

}
}

add_action(‘init’, ‘login’);

add_shortcode(‘bbLoginForm’, ‘loginform’);

Read more here:: Pass $_POST data to a function

Leave a Reply

Your email address will not be published. Required fields are marked *