I'm new to WordPress. I have a jQuery code like this... in my header.php file.

       type: "POST",
       url: "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
      data: "name="+username+"&pwd="+password,
       success: function(html){    
      if(html=='true')    {
       jQuery("div#result").css('display', 'inline', 'important');
       jQuery("div#result").html("Login Successful");
       jQuery("div#result").css('display', 'inline', 'important');
       jQuery("div#result").html("Wrong username or password");

       } });
    return false;

and here is the php code in the same file (header.php):

      $uName = $_POST['name'];
      $pWord = md5($_POST['pwd']);
      $results= $wpdb->get_results("select * from wp_users where user_email='".$uName."' and user_pass='".$pWord."'");
       echo 'true'; 

      else {
        echo 'false';

I'm not able to send request to server, i got a 404 error.

What am i doing wrong?

  • Hi. Are you trying to access your website using www.example.com?name=Jay&pws=kapoor? Try adding a forward slash to it : www.example.com/?name=Jay&pws=kapoor. This might save you from a 404 error.
    – Johansson
    Commented Jun 6, 2017 at 8:39
  • If you get a 404 error, you are able to send a request to the server. But apparently it is an invalid request.
    – cjbj
    Commented Jun 6, 2017 at 8:54
  • my question was how to link php file.. i mean what url should i provide in ajax url
    – Jay Kapoor
    Commented Jun 6, 2017 at 9:36

1 Answer 1


Ajax url should be set to wp-admin/admin-ajax.php as ajax requests should be handled via wp-admin/admin-ajax in wordpress. if ajaxurl is not defined you can get it by defining html attribute data-url as data-url="<?php echo get_admin_url().'admin-ajax.php'?>".

      username = $("#userName").val();
      password = $("#passWord").val();
      ajaxurl  = $(this).data('url');

      type: "POST",
      url: ajaxurl,
      data : {

            name     : username,
            password : password,
            action   : 'check_user_login'

       success: function(response){    
         if(response =='true')    {
             $("div#result").css('display', 'inline', 'important');
             $("div#result").html("Login Successful");
            $("div#result").css('display', 'inline', 'important');
            $("div#result").html("Wrong username or password");


Now Create your ajax function as

function user_login(){

   //do your stuff here
    global $wpdb;
    $hash = md5($password);
    $query = $wpdb->prepare( "SELECT * FROM `wp_users` WHERE `user_email` = %s AND `user_pass` = %s", $username, $hash );
    $result = $wpdb->get_results($query);
     if (count($result)==1) { echo 'true';
     echo '<span>Login Successful</span>'; exit(); 
    } else if(count($result) < 1) {
     echo 'false'; exit(); }

Now the ajax function should be hooked to hook wp_ajax_* and wp_ajax_nopriv_*. Here, * is your action value i.e data.action so it becomes

  • Try to put this code in your funttions.php file ini_set('display_errors', 1); and check console if any error message is displayed. that's all for today. Good luck :) Commented Jun 6, 2017 at 11:26
  • i think your sql query should have user_login instead of user_email as you are taking the username input not the email if you are using default wordpress wp_users table Commented Jun 6, 2017 at 11:45
  • can u tell me how to compare wordpress hashed password.
    – Jay Kapoor
    Commented Jun 7, 2017 at 4:06
  • Go to this [link]codex.wordpress.org/Function_Reference/wp_hash_password here, go to example to see how it is to be used. Hope this works for you. I don't know what you are doing but you can use builtin login system instead of creating your own login system. Commented Jun 7, 2017 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.