passing variable between functions
-
I am working on custom login plugin. I am trying to pass the username and password variables to the function validatedata() to check if the fields are empty or not. if either fields or both are empty then display the correct error message.
I cannot seem to pass the username and password variables to the validate function. How can accomplish this using hooks or is there another way to achieve the same thing? (at the moment I am using global variables).
//this function displays the custom login form
function loginform(){ errormsg(); return ' <form method="post" action="'.esc_url("https://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> '; } //this function will basically check if username and password fields are empty function validatedata(){ global $username, $password; $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'); }else{ $errors->add('no_error', ''); } return $errors; } //this errors msg will need to be triggered when the user clicks on the login buttion function errormsg(){ $formErr = validatedata(); if(is_wp_error($formErr) && !empty($formErr->errors)){ foreach($formErr->get_error_messages() as $errMsg){ echo '<div>'. $errMsg .'</div>'; } } } //the function where the login process will occur function login(){ if(isset($_POST['login'])){ global $username, $password; $username = sanitize_text_field($_POST['username']); $password = sanitize_text_field($_POST['password']); echo $username.' '.$password; } } //fires up before wordpress is loaded, I guess? add_action('init', 'login'); //used to add the custom login form to a page or post add_shortcode('bbLoginForm', 'loginform');
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘passing variable between functions’ is closed to new replies.