2

Hi I am trying to have live search which searches for existing emails when a user registers. Below is my jquery script

$("#wjp_register input#user_email").change(function(){
    console.log("lol");
    var email=$("#wjp_register input#user_email").val();
      $.ajax({
            type:"post",
            dataType : 'JSON',
            url:"/wordpress/wp-content/themes/tsf/wpjobboard/job-board/check.php",
            data:"email-address="+email,
                success:function(result){
                if(result==0){
                    console.log(result);
                    $(".error").html(" Username available");
                }
                else{
                    console.log(result);
                    $(".error").html("Username already taken");
                    proceed = false;
                }
            }
         });

});

When i check the response in the developer console, the email is echoed successfully but the wordpress get_user_by doesnt seem to work.

My php script

<?php
$mm = $_POST['email-address'];

if ( isset( $_POST['email-address'] ) && ! empty( $_POST['email-address'] ) ) {

  //sanitize the data
  $email_addr = trim( strip_tags( stripslashes( $_POST['email-address'] ) ) );
  echo $email_addr;//This is printed successfullt
  echo "<br>";

  //This below part doesnt work :(
    if( false == get_user_by( 'email', $email_addr ) ) {
        echo "Doesnt exist";
    } else {
        echo "exists";
    }

}
?>
8
  • get_user_by doesn't return boolean... it returns an object of a user. The docs show an example: codex.wordpress.org/Function_Reference/get_user_by Commented Jan 8, 2015 at 14:31
  • 1
    @BigChris "object or false if no user is found" Commented Jan 8, 2015 at 14:32
  • Whoops. My bad. Sorry! Would help if I read the docs to what is returned... perhaps you need to check is false with === rather than == ? Commented Jan 8, 2015 at 14:34
  • 2
    @ash Since you are echoing "Doesnt exist" or "exists", those are the possible values for result in your success callback. result==0 will never be true because you're not sending 0 back anywhere. Also, what exactly do you mean when you say that it "doesn't work"? What specifically isn't working? What are you expecting vs what is actually happening? It is a bit unclear if an unexpected value is being returned by get_user_by or if an unexpected value is being sent to the javascript callback. Commented Jan 8, 2015 at 14:35
  • 1
    Are you sure that you have included wp in your check.php file? Otherwise the function get_user_by doesn't exist. Commented Jan 8, 2015 at 14:40

1 Answer 1

1

So here is the correct way to use WP from an external file:

Include wp-blog-header.php if you need all of WP and want to fire all of the default hooks and actions.

define('WP_USE_THEMES', false);
require('./wp-blog-header.php');

Includ wp-load.php if you only need the WP functions. It doesn't call wp() or invoke the template loader. So it's more lightweight!

define('WP_USE_THEMES', false);
require('./wp-load.php');
Sign up to request clarification or add additional context in comments.

2 Comments

Whats difference does it make if use wp-blog-header.php or wp-load.php?
I modified my comment. In your case it's enough to include wp.load.php

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.