0

I am making a WordPress template, mytemp.php in wordpress 3.9. In this I can use wordpress functions. For example, below works perfectly.

    $(document).ready(function() {
        alert(<?php echo get_current_user_id() ?>);
    }

However, this template file calls some ajax scripts too depending on user input. Over there it doesn't work. For example, below statement returns fatal error "call to undefined function get_current_user_id"

    $sql = 'SELECT x,y FROM table WHERE user_id = ' . get_current_user_id();

I am guessing, I need to tell the ajax script to include some wordpress file or some global variable but I am not sure how.

4 Answers 4

1

I solved it. All I needed to do was to have below include statement.

   include '../../../../wp-load.php';

By including this, it started recognizing the function get_current_user_id()

Sign up to request clarification or add additional context in comments.

Comments

1

Try to include wp-load.php file in ajax file, after including this file wordpress functions are working properly in your ajax file or you need to write a plugin like ajax.php, and then use this plugin file for your ajax file.

After that you can access the wp functions.

Hope it'll helps.

Comments

0

Try this. I just answered a question not too long ago with this style of sql syntax using the function get_current_user_id();

$sql = "SELECT x,y FROM table WHERE user_id = %d", get_current_user_id();

7 Comments

Nopes. It didn't work. Started giving error "Parse error, unexpected ','". It seems it is using %d not as a placeholder but as part of the string.
It has to be inside double quotes like I have it, not single quotes like you do or else it will use it as %d
Even with Double quotes it is taking %d as part of a string. Do I have to use some escape character...I am afraid, even then, primary problem will remain as it is. Even $x = get_current_user_id() is not working.
can you update your code to show all of the sql connection code (except you login credentials of course)? I may be able to figure it out. Basically I just want to see a bit more of the code related to this part of your template.
Following is the simple php with no sql at all. Even this throws the same error. <?php $x = get_current_user_id(); echo $x; ?>
|
0

Another solution would be to establish a contract between your ajax script and the javascript that posts to it. So, in your calling php script, you could set up some JSON in your header:

<script>
  <?php echo "wp-data = {current_user_id: " . get_current_user_id() "}"; ?>
</script>

And then put it in your ajax call:

$.ajax({
   url: "http://yourdomain.com/process_wp_ajax.php",
   method: "post",
   data: {
      . . . //your post data
      current_user_id: wp-data.current_user_id;
   })
   .success(function(response) {})
   .fail(function(response) {} )
   .complent(function(response, status) {};

Your receive should expect "current_user_id" to be on the POST (or GET).

This involves more work, but you save yourself from loading the WP framework each time you make an ajax call.

Don't use the php above if you want to set multiple values. Set an object instead and just call json_encode on it:

<?php
    wp_object = (object) array('wp-val1' => wp_func1(), 'wp-val2' => wp_func2());
?>
<script>
    <?php echo "wp-data =" . json_encode(wp_object) . ";"; ?>
</script>

DRY, just pass the wp-data object directly in your ajax:

$.ajax({
  url: "http://yourdomain.com/process_wp_ajax.php",
  method: "post",
  data: { 
     . . . 
     wp-object: wp-data,
   }
   . . . 

So in your calling script, you would eventuall arrive at:

$sql = "SELECT x,y FROM table WHERE user_id = %d", $_POST['current_user_id']

or `$_POST['wp-object']['current_user_id']

(of course you would set the $_POST value in a ternary prior to this right?

Comments

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.