I'm currently using php and javascript in my Wordpress plugin. The idea is that javascript fetch updates the database. Currently I have a page with "server" shortcode and I fetch this page from the "client" page. This server page receives the fetched data (POST request) and does some database updating and returns a new value in a small XML data patch (added via shortcode).
The system works, but I'd want to improve it. Preferably, so that the "server" fetch would not return the entire page (headers, footers, css and all), but only the data file (i.e. application/xml instead of text/html).
Can I accomplish this while still maintaining my access to global $wpdb and other features of Wordpress? I tried another way: I managed to expose a php file by adding it to pluginpath/includes, but I am unable to use $wpdb there, which is how I access my database.
So, requested help, either, or: (a) how do I enable use of $wpdb in the small php "server file" (that does database fiddling) or (b) how do I make the Wordpress page suppress all other data than the data patch (small XML) that I want to pass?
Aimed result: as few bytes as possible moving in the internet. (So, bytes instead of kilobytes.)
CodePudding user response:
You want to look at - https://developer.wordpress.org/reference/hooks/wp_ajax_action/
Since you are using JS anyway you could pass what you need to the endpoint then return specifically what you need. Without knowing more of what you are trying to get. You could specifically request it back as json etc.
