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 to ajaxify all pages of my Wp Site
- jquery not calling php function
- Search for categories
- Modifying a .js Function for Print [QZ Tray, WooCommerce POS]
- How to hide CSS by default and show on button press
- AJAX in plugin wp_send_json() sending html
- Uncaught TypeError: jQuery(…).flipster is not a function in wordpress
- Best place for if/else piece of code related to custom plugin?
- Making sure that a plugin’s scripts and stylesheets are included for dynamicaly loaded content
- jQuery for custom plugin not working with Divi theme
- Ajax call returning 0
- Ajax with plugins returns 0
- WordPress + JavaScipt + AJAX + MySQL: insert query for form
- Ajax fail and get 504 error
- Ajax Search Lite: Customize Autoscroll
- admin-ajax.php warning max input vars exceeded on layered pop plugins [closed]
- WordPress Integration with Google Groups
- Best place to load wp_ajax_the_ajax_hook action in plugin
- jQuery.post not working within shortcode – WordPress Ajax
- Create tabs in admin options page from custom post type loop
- How to load wp_editor via AJAX
- Redux Framework: Page freezes on typing in Editor
- Init plugin again after ajax call finish
- jquery tinymce and bundled wp plugins
- Using AJAX to generate front end / viewer end pages
- How to prevent plugins from loading jQuery
- JQuery plugin that works with Google 1.10.2 and not with WordPress 1.10.2
- How to handle cookies from a WordPress plugin on a cached page?
- Ajax : Call undefined function plugin_function() … can’t call any plugin function
- Loading a purchased jquery plugin in a wordpress theme
- Using window.onload with Ubermenu
- wordpress file upload with ajax when site is ajaxyfi
- AJAX search as you type? [duplicate]
- jQuery | JJ Carousel Plugin – Can’t set speed [closed]
- WordPress api call using wp-ajax returns error 400
- Create shortcode to echo javascript
- Integrating jQuery Datatables with Editor into wordpress admin
- AJAX call – failling to call do_action
- Using wpdb without loading all plug-ins via wp-load.php
- Stop WordPress processing Javascript as text
- Jquery plugin not working on single pages
- What are the Best Practises When Using AJAX in Plugin Development?
- Jquery NoConflict Problem
- this jQuery function works perfect in static html page but not on wordpress
- How to include this jQuery File in wordpress?
- Admin AJAX doesn’t work in plugin admin page – Even though code is copied verbatim from WordPress Codex
- Call plugin with php function
- “Conflict” with jQuery (or its plugins)?
- How does one include the JQuery UI plugin to wordpress and enque it correctly in a plugin form?
- Render Modula Plugin Shortcode On Ajax Request