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(){
return ‘
<form method=”post” action=”‘.esc_url(“http://findevsvr01:83/index.php/login/”).'”>
<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”/>

function validatedata(){
$errors = new WP_error();
if(empty($username || $password)){
$errors->add(‘required_fields’, ‘All Fields Required’);
$errors->add(‘username_required’, ‘Please Enter Username’);
$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(){
$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 *