Retrieve post info within AJAX helper function

I have a button within a post which a user can click. Once clicked, an AJAX call is made.

My question is, within my PHP AJAX helper function (located in my theme’s function.php) how can I retrieve, for instance, the ID of the post that the request was sent from.

Read More

I can get the ID from the markup and pass it in the AJAX call but it seems messy, i.e.

<article id="post-12">
<!-- article content -->
</article>


// JS
var postID = $("article").attr("id");

Is there a better way? Some kind of global that is accessible perhaps?

Related posts

Leave a Reply

2 comments

  1. If this is within the loop, you can use the_ID() or get_the_ID() and pass the value. I don’t see how a helper function can know what the post is unless you tell it explicitly.