The code you have shared can work in core PHP and not in WordPress. Using AJAX in WordPress plugin is explained in WordPress documentation. Please refer – https://codex.wordpress.org/AJAX_in_Plugins
I have tried this and it works well.
Related Posts:
- Help making my pagination plugin better
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- jQuery AJAX call not executing in Plugin
- Why is my ajax call refreshing the page?
- What’s the point of using WordPress’s built in admin-ajax.php?
- Admin-ajax.php is dying “0” without processing function
- How to create popup same as wordpress popup feature
- Only execute jQuery function(on document ready) on the page has shortcode from plugin [duplicate]
- Ajax with jQuery UI dialog not working
- jQuery Plugin to use WordPress functions in AJAX request
- Check if email address exists front end with AJAX in a plugin
- Form isn’t inserting data into database with ajax plugin
- How to put JQuery/Ajax inside shortcode?
- Ajax contact form widget plugin data not insert in database
- How can I inject options into an select tag inside the widget form?
- Display wordpress post’s in popup?
- Using color picker in plugin, does input attribute order matter?
- Post Comments with Ajax in WordPress
- AJAX fileupload – TypeError: not a function ajaxSubmit()
- wordpress add_action() issue in ajax call
- How to display checked posts on another page over AJAX? (like comparasion style)
- How to include my Ajax calls in one function instead of calling different ones every time?
- Using AJAX to run SQL statement and populate dropdown
- Ajax request sends url rather than data
- wc_get_template_part doesnt display the content [duplicate]
- Jquery POST Ajax Return null data in wordpress
- How do I reinit WordPress plugins dynamically using jS?
- WP Ajax DB issues
- Cannot pass value variable to WP AJAX functions
- Ajax Call not Working in Plugin
- jQuery Ajax not loading page with ACF fields
- JS working when used normally but not in wordpress
- checkout stripe with plugin contact form
- WordPress Ajax request “Failed to load resource: the server responded with a status of 400 ()”
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- Overwriting Plugin’s Ajax callback function from theme
- Help with customising love it plugin (use within foreach)
- WordPress Feedback popup with date saved into table
- Not getting result using ajax on wordpress
- WordPress ajax not working in registration
- AJAX doesn’t pass variables to the php function in the plugin
- Like/Dislike Bar not working when updating
- Ajax, jQuery and WordPress
- Hyperlink to execute PHP
- How to get site homepage from plugin?
- WordPress plugin ajax post parameter
- Load css classes after using ajax calls
- Jquery post responses 500 error after some time and lastly an 503 error
- Ajax button “Load more” is not loading correct language version posts for logged out users
- Ajax response from Media Selection does not update ALL information more than once
- How can I make default jquery version of WordPress “async”?
- Handling jQuery Component Collision
- How to Add jQuery Infinite Scroll to My Custom Archive Page
- WordPress Enqueue Script Chaos (jQuery & Foundation.js)
- Ajax Load More and Masonry: is it possible to load admin-ajax.php in the front end?
- Check javascript file Proper way in functions.php
- WordPress Plugin development Call to undefined function jQuery()
- How can I process xml file on upload?
- WordPress 3.5 Media Manager – add a button
- Handling jQuery Component Collision
- ajax front-end increment views on click
- How to Create a Custom Panel and Fields in Post Page [Plugin]
- Change upload folder for a CPT
- Theme is Causing Ajax Conflicts for a plugin I cant identify it
- Search for categories
- Uncaught TypeError: jQuery(…).flipster is not a function in wordpress
- What are the Best Practises When Using AJAX in Plugin Development?
- “Conflict” with jQuery (or its plugins)?
- Does having more than 30 Admin Ajax affects site performance (plugin)?
- How to track clicks
- Adding media upload button to User Profile page (following a tutorial)
- where do I send my ajax calls
- Ajax save data to database on document ready, no data being saved
- How to get specific string/value from an array? php [closed]
- Passing PHP Variables to AJAX Loaded Pages
- Why jquery option is not working when pass value using wp_localize_script?
- “switchEditors is not defined” with wp_editor() in jQuery UI tabs
- grab or load text on demand
- sliding/fading header plugin or approach suggestion
- admin-ajax.php 500 error when new users try to register
- Send Array from JS to php file then insert it in wp database through a plugin
- Cannot read properties of undefined (reading ‘firstChild’) using photoswipe in product page
- Trigger jQuery to add events after AJAX-loading table in admin plugin
- $wpdb not working wordpress plugin ajax call
- Conditionally load public and admin code with AJAX working on both sides
- Scroll to ID broken in main menu
- Dynamic input fields in WordPress Plugin
- WordPress ajax error 400 bad request for sending data to remote site [duplicate]
- Get shortcode attribute for AJAX call
- Why can’t I call a function from inside my registered AJAX function?
- Add HTML between feed items
- Javascript not working
- ajax request not returning the result
- How do I convince this button to do something when it is clicked?
- ajax sends data to plugin
- “The Events Calendar” Issues in WP 3.2
- FacetWP: Plugin breaks button/modal functionality inside searchable content area
- TypeErrors with Yoast, jQuery in conflict?
- Count div with class (.length) only shows 0 [closed]
- wordpress admin-ajax bad request 400