WordPress – how to get ‘page’ variable from external file?

I use external file for handling AJAX calls in WP plugin. For it to iterpret WP methods, I made a following hack:

require( '../../../wp-blog-header.php' );
header("HTTP/1.1 200 OK");

Problem is, it seems that I can’t get any variables, specific to the state of the page from it. For example, get_query_var('page') always comes back empty.
Is the only solution to put Javascript into plugin main .php file, and get the ‘page’ variable from there, feeding it to the script (it should certainly work via hooks)? I want to avoid mixing PHP and JS at all costs.

Read More

P.S. For the greater truth, I failed to retrieve that variable. I can still get the maximum page number, though, calling $q = $GLOBALS['wp_query'];
$max = $q->max_num_pages;
. Accepted answer is certainly correct, but I believe this is a bug. I’ll ask guys from wp-hackers when I have time, and, hopefully, update this question.

Related posts

Leave a Reply

2 comments

  1. It’s a bit long for a comment so I add it as an answer. Bear with me if it does not contain the solution:

    get_query_var('page') will only return something if there is an actual query running. When you request your plugins PHP script directly this most certainly is not the case.

    So the question is, to which sort of page variable are you referring to? Is it probably $_REQUEST['page']? That’s just guessed, maybe you can add to your question more information about the page which is firing your AJAX call.