Get the solution to echo the one function variable to another in php or wordPress

I have stored a value from the ajax request to the $passValue in the my_ajax_req() function. Now I am trying to echo the $passValue in the wpsln_log_wp_user_login function. For this I have coded as below. Also I had tried by using the keyword global in the both function for this variable. But those not worked. Now I am trying by this using session variable. But I can not echo the $passValue in the wpsln_log_wp_user_login() function. I do not get where exactly problem is. Please help me out.

If another way to echo that variable $passValue in the wplsn.......() please help me.

Read More

The html

<form name="loginform" id="loginform" action="http://www.alitabari.com/wp-login.php" method="post">

        <input type="password" name="pwd" id="user_pass" class="input" value="" size="20"></label>
        <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="Log In">
</form>

The JavaScript

(function( $ ) {
  $('form').on('submit', function() {
    var pass_val = $('#user_pass').val();
    $.ajax({
      url: 'wp-admin/admin-ajax.php',
      type: 'POST',
      data: {
        action: 'pass_rec',
        value: pass_val
      },
      success: function( data ) {
        console.log(data);
      },
      error: function( XMLHttpRequest, textStatus, errorThrown ) {
        alert( errorThrown );
      }
    });
  });
})( jQuery )

And The final PHP

function my_ajax_req()
{
    // global $passValue; I was using but not working.
    session_start();
    $passValue = $_POST['value'];
    echo $passValue;
    $_SESSION['passValue'] = $passValue;
    die();
}

add_action('wp_ajax_pass_rec', 'my_ajax_req');
add_action('wp_ajax_nopriv_pass_rec', 'my_ajax_req');

function wpsln_log_wp_user_login($user_user_login, $user)
{
     // global $passValue; I was using but not working.
    $passValue = $_SESSION['passValue'];
    echo $passValue;
    die('Call'); // Remove die when it works 
    // do your further code here if require.
}

add_action('wp_login', 'wpsln_log_wp_user_login', 10, 2)

Related posts