I have a jQuery code that is going to check when the user is near the bottom of the page. That’s not the problem though. This jQuery is going to send a AJAX request, giving it some details on what to load when the user is near the bottom of the page. The code looks a bit like this at the moment:
$("<div>").load('?ajax=y&offset=something', function() {
$(".empty-div").append($(this));
setTimeout(function(){ console.log('after', $(document).height()); }, 0);
setTimeout(function(){ console.log('after', $(window).height()); }, 0);
});
My main problem is that I don’t know what to query or how to go about sending the information to the PHP function in functions.php. For example, I have at the moment this as my PHP function (until it’s working):
function get_posts_page() {
if(isset($_GET['offset'])) {
echo"Hello!";
}
}
I’m aware the wordpress has add_action
and all that but I have no idea what I would apply as an action to either function to make the PHP recieve the data the Javascript is sending. Is there a URL where all functions are parsed or something? Thanks for any help in advance. So how do I get the data from the Javascript to the PHP function in functions.php, in my theme directory?
I just made a video to show you how to use the
add_action
request in WordPress. You can watch it here.Here’s my javascript
And the php that I used in
functions.php
Reference: http://codex.wordpress.org/AJAX_in_Plugins
You’re trying to call a PHP function from Javascript, correct?
You’ll need some logic on some page which calls
get_posts_page()
. Either you can create a new pagegetPostsPage.php?offset=
or you can put some logic infunctions.php
, something likeHowever, the former approach is recommended; you don’t want someone to be able to modify the
function
parameter and accessdeleteAllPosts()
maliciously.Therefore:
And remember to fail gracefully (do not expose an error message) if ‘offset’ is not set, or whatever.