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?