You can output your javascript via wp_add_inline_script()
. This way you can set the post”s id and AJAX URL before outputting the code:
wp_add_inline_script('my-js', '
jQuery.ajax({
\'url\' : '.admin_url('admin-ajax.php?action=my_action').'
data : {
\'id\' : '.get_the_ID().'
}
});');
Note that you need to hook to an existing js file to be able to add inline script, so my-js
must be a valid enqueued script.
Related Posts:
- Update user meta using with ajax
- WordPress AJAX Login Screen
- How to link WordPress heartbeat to ajax form
- How to correctly load wordpress in a non WP script for AJAX request
- Insert wp_editor on front-end with AJAX?
- Help with AJAX front end comment moderation
- Using AJAX with Forms
- Three level taxonomy dropdown frontend
- Performance optimization of tree like structure
- Is it secure to use admin-ajax.php in front?
- Best way to use ajax front-end?
- wp_ajax declaration confusing for Front end
- How to prevent my external API call from being called by anyone but me (my site)
- Update user meta via ajax from frontend, saving issue
- Edit user meta on front-end via AJAX
- How to test if in dashboard, bypassing Ajax quirk
- ajax page template
- jQuery’s .on() method combined with the submit event
- Why use admin-ajax.php and how does it work?
- Why is die() used at the end of function that handles an Ajax request?
- Making my AJAX powered WordPress Crawlable
- Using Backbone with the WordPress AJAX API
- Why is a 500 error generated by admin-ajax.php not going into the Apache error log?
- admin-ajax.php doesn’t work when using POST data and Axios
- Custom Form with Ajax
- How to process ajax requests correctly using ajax plugins
- How do I hook an Ajax request into a PHP callback?
- Gutenberg – how to correctly perform ajax request on backend
- get_template_part execute with ajax
- How to add WordPress nonces to ajax request
- How is it possible that the function of the test page works, but it does not go live?
- How declare Ajax functions ussing SHORTINIT
- jQuery Autocomplete not working with wp_localize_script
- Load custom formatted comment with AJAX: reply link isn’t rendered?
- Is there a way to optimize function that is used for returning data in an ajax-call?
- Ajax returning correct data BUT at wrong place
- Creating a Front-end based User Search
- Edit post meta with checkboxes on front end
- get_posts empty when called via Ajax
- admin-ajax.php mixed content
- WP-API and Basic Auth returning 403 on POST but not GET
- Is there a hook to process a backbone restful PUT request inside wordpress?
- Check if username exist with AJAX
- How to localized one js file for different actions?
- WooCommerce: Translation lost on AJAX call in Checkout page [closed]
- How to add WP API and JS featured image attachment
- json encode not work in wordpress
- wordpress and add actions to button by coding
- Get Author Post on author.php with AJAX
- Ajax in plugin settings page returns 400 Bad Request
- Admin-Ajax Error
- WordPress Ajaxify Problem with Scroll-to [closed]
- Stop ajax listener from sending empty parameter to function
- AJAX and -1 response
- Ways to load admin-ajax faster without initializing all plugins?
- How to make registration form ajax?
- Specify ABSPATH in jQuery url
- WordPress action – Pass arguments into action in an AJAX call?
- 403 error When i send ajax request on WordPress website
- Ajax Call 400 Bad Request error with POST request but not with GET request
- While Ajax is working well, media upload isn’t showing the imagines
- How can I call an api before user login
- Problems with wordpress nonces in Ajax call
- Get post content and show it in a div
- Output multi-steps form results in same page
- why does not work ajax that add_action registered in wordpress
- What WP-API authentication method should I use to interact with anonymous / not-logged visitors?
- How to call ajax in plugin file
- Tabbed feeds with ajax vs. without on homepage [closed]
- Get title and featured image using Ajax
- javascript onClick update user_meta from jquery.ajax
- How to ignore WP_ERROR caused by “get_the_excerpt” method in an AJAX call?
- WP_Query is not received in Ajax
- My function containing a mysql query launched by ajax is not working in wordpress. What am I missing?
- How do I mitigate replay attacks when talking about actions that shouldn’t happen twice?
- Updating Jquery object with newly created elements after AJAX call
- wordpress ajax search posts
- Ajax call from Plugin using Class
- enqueue style using admin-ajax.php
- How to safely pass post_id and user_id via AJAX to the backend (prevent user from changing it via JS)?
- AJAX form not working, still reloads on submit
- Call public static method from ajax ‘wpcf7_mail_sent’ hook
- How to get the admin page slug using wp_loaded hook?
- How to paginate Ajax result
- Cannot access class properties from ajax call in wpordrpess
- Having a self updating list
- Unexpected WordPress search results
- WordPress Get Header and Footer using in Admin Area
- Tie the sending of an ajax request to WordPress hooks
- AJAX: admin-ajax.php is adding extra content to my script’s response – how to exclude header, footer, etc.?
- apply_filters, EMBEDS and AJAX not a friends? [duplicate]
- Using Javascript Callback from plugin in a theme
- Why does my Ajax Get request give a 400 bad request?
- How to get data with Select AJAX PHP
- Add “load more” functionality to an AJAX response
- Bad Request 400… jQuery ajax post of json data to wordpress admin-ajax.php
- weird Internal Server Error – no error log produced
- Why are the most recent posts not appearing in a fetch request, unless I’m logged in?
- Successful ajax call returns lots of whitespace and text of code with 0
- How to submit a button automatically after every scheduled hours?