• Resolved ggedare

    (@ggedare)


    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)
  • Moderator bcworkz

    (@bcworkz)

    When you are coding your own, it’s best to pass required values as function parameters.

    $username = sanitize_text_field($_POST['username']);
    $password = sanitize_text_field($_POST['password']);
    $user = validatedata( $username, $password );
    if ( is_wp_error( $user )) { wp_die( $user->get_error_message );}
    // User checks out -- log them in
    
    function validatedata( $user, $pwd ) {
      // validate passed values
      // if data validates, try to get WP_User
      // return either WP_User on success or WP_Error on fail
    }

    Globals should be seen as a last ditch crutch for when you’ve coded yourself into a corner. (However, WP makes extensive use of globals) Another way to pass values when using function parameters is out is via class static properties combined with static getter methods. Then you can get or set values with something like My_Class::get_my_value() and My_Class::set_my_value('bcworkz')

    It can be argued these class methods are merely elaborate globals. Perhaps, but it’s far more structured and better avoids name collisions, avoiding the drawbacks of simple globals.

    Are you aware that there are WP functions that do all of this checking and logging in for you? (wp_signon() for one) If you are avoiding these built in functions for the sake of learning, that’s fine, but review the source code of the WP functions to be sure your code is doing all the same checks. You don’t want to compromise on security ??

    Oh BTW, the “init” action fires right after WP is fully loaded, but before the request is processed. Trying to use WP functions before “init” fires is unreliable.

    Thread Starter ggedare

    (@ggedare)

    @bcworkz, thanks for the reply. Now I fully understand the purpose of the init function. I read about it but did not fully delugve into really understanding it’s usage.

    Thanks for the Response

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘passing variable between functions’ is closed to new replies.