database – Custom AJAX Endpoint not returning any result
Hello I’m new to WordPress and I’m trying to create an ajax endpoint that outputs JSON so I can access it through jQuery Datatables. I’m using Air Light theme for my current site and what happens when I go into /wp-admin/admin-ajax.php?action=ajax_endpoint
is that it just gives me 0. I tried querying posts, users, and my custom tables they all give me 0 even though I have a status code 200.
I’m wondering if there’s something wrong I’m doing here. I’ve also tried putting an echo somewhere in the function and it does not show up.
functions.php
namespace Air_Light;
/**
* The current version of the theme.
*/
define( 'AIR_LIGHT_VERSION', '8.3.2' );
/**
* Theme setup code
*/
add_action('wp_ajax_ajax_endpoint', 'ajax_endpoint');
add_action('wp_ajax_no_priv_ajax_endpoint', 'ajax_endpoint');
function ajax_endpoint(){
global $wpdb;
$results = $wpdb->get_results('SELECT * FROM table');
echo json_encode($results);
wp_die();
}
Leave an answer