When you send an AJAX request, wp-admin/admin-ajax.php
is called, not your template file. This separate request has no knowledge of all existing code in all existing templates, because they aren’t even included.
What is included: the theme’s functions.php
and plugin files. This is why your callback works in a plugin.
As a rule of thumb: templates should never contain business code. No function or class declarations, no registration of callbacks. They are templates, they should just add markup to data, nothing else.
Related Posts:
- Using Ajax with a Class file
- Are ‘wp_ajax’ and ‘wp_ajax_nopriv’ exclusive to authenticated and non-authenticated users?
- How to modify wp_ajax function?
- Ajax Request not coming back to class
- wp_ajax action is not running
- add_action(‘wp_ajax_[action name]’, myfunction) problem
- add_action and Ajax
- WordPress Ajax Problems
- admin-ajax.php ” Missing argument 2″ warning
- Code run fast on a cronjob but slow with a do_action or ajax call
- WordPress ajax-action how to return content
- WordPress action – Pass arguments into action in an AJAX call?
- why does not work ajax that add_action registered in wordpress
- wp_ajax action is not run when ajax trigger
- Sharing variables in both actions wp_footer and wp_ajax
- Ajax callback not work
- admin-ajax.php calls fail if referrer is sent with 500
- How to run an ajax call in elementor editor
- do_action won’t work in ajax callback
- Ajax call works for logged in users and returns “Bad Request” for guests [duplicate]
- Its possible to ajax update the content of the admin custom post type list
- Trigger action via button
- Why function called by admin-ajax executes synchronously?
- where does my function output from load-* go?
- Static var overwritten with WP AJAX action, works fine without AJAX
- Access-Control-Allow-Origin error sending a jQuery Post to Google API’s
- Show spinner GIF during an $http request in AngularJS?
- ASP.NET MVC controller actions that return JSON or partial html
- How to check if I am in admin-ajax.php?
- Ajax in a settings page (update_option is undefined)
- Ajax and autocomplete
- admin-ajax.php vs Custom Page Template for Ajax Requests
- Saving (Updating) Post / Page Edits With AJAX
- Is there a hook that fires before an ajax call?
- AJAX request from Chrome Extension to WordPress Website
- Minimal WordPress load for only `get_option` to work (because ajax…)
- Why is admin ajax reloading my page
- Admin Ajax always return 0 on form submit
- Why this plugin doesn’t work with media upload page?
- Confused on AJAX submit form through page template
- Ajax call in wordpress not working for subscriber user in front site
- Solve cors problem using rest api or ajax
- Why does WordPress Heartbeat login not refresh the nonces?
- Why would admin-ajax.php redirect to the home page for logged out users?
- Insert wp_editor on front-end with AJAX?
- Buddypress: Load Ajax Without Template Pack [closed]
- Implementing an AJAX POST API call in wordpress
- Woocommerce Ajax Add cart not working
- I have to post data by AJAX in wordpress to another Website
- Using ajax with wordpress
- Using foreach inside an ajax function
- WP_User_Query ignoring ‘meta_query’ arguments
- Ajax not working es expected (Returns 0)
- Ajax image upload with media_handle_upload and form.js
- Edit a different page in WP Customizer
- Get the_content with ajax
- Caching-Plugins and Ajax-Page-Parts
- Ajax loading duplicate post
- Admin ajax error 400 when passing select value to populate another select
- How do I detect in which page ajax_query_attachments_args is loaded?
- Ajax post returning full html page as response
- Which allowed API hooks work to add wp_ajax action?
- AJAX request randomly stop working and returns error 400
- Sending variable from ajax on form submit
- Ajax store response json in variables
- Performance optimization of tree like structure
- How can I set cookies on both secure and non-secure origins at the same time?
- Variable Products Being Added to Cart with AJAX on Shop and Category Pages
- Add/remove action on jQuery toggle
- Is it secure to use admin-ajax.php in front?
- ajax is returning 0
- merge wp rest api query to get posts per category does not work
- WP AJAX post filter > do something with empty value
- How to make a fetch() POST request to wordpress rest api?
- Load more posts in the same category – Ajax + Timber
- How to jQuery Ajax show new data from successful insert?
- is there’s a way to get the next page content generated by page break block with ajax call?
- Merge PDF files from post custom fields(ACF) into one PDF file and respond back the created PDF file, WordPress(AJAX Request)
- How to get next and previous post into ajax formed modal windows?
- Passing value from select form with Ajax not working
- Sending email through Ajax without page refresh hangs on admin_ajax.php
- Is there a public ajax endpoint?
- Why Ajax Doesn’t Work?
- How can i do public ajax call?
- How to prevent multiple post with same meta value being created simultaneously in WordPress (with ajax)
- Frontend AJAX Media Upload returning 404
- Can’t GET Variable from AJAX URL
- How to update cart total after AJAX success
- How to implement AJAX within a class storing shortcode atts as class variables
- How do I display posts of a specific day?
- (Frontend AJAX) get post data WITH image thumbnail?
- Ajax Function call is always returning 0 in front end(without plugin) [closed]
- AJAX Post from same domain to a wordpress page
- Ajax live search, “No products found” when the title contains apostrophes and quotations
- Add Ajax loaded posts to existing posts
- Change search to outpout category name instead of Post type name
- ‘Dehighlighting’ navigation once clicked
- wp_localize_script not create variable in head section
- how reduce fetch/XHR response time
- wordpress ajax bad request 400