Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - how to get 'page' variable from external file?

Tags:

php

wordpress

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.

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.

like image 599
Arnthor Avatar asked Dec 05 '25 01:12

Arnthor


1 Answers

If you're using get_query_var('page'), then make sure that you've actually run a query on that page prior to calling the function.

like image 200
Sam Starling Avatar answered Dec 06 '25 15:12

Sam Starling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!