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.
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?
Use
wp_localize_script()
to declare globals.Example:
You could store your query results there and access them later.
If this is within the loop, you can use
the_ID()
orget_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.