I am running a theme on WordPress. In this theme, I am making some AJAX calls where I am expecting some response. (http://example.com/capis/v0/packages/)

To serve above http calls I am using silex as http server.


require_once __DIR__ . '/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Silex\Application;

$app = new Silex\Application();

$app->POST('/capis/v0/packages/', function(Application $app, Request $request) {
            # logic            
            return new Response('HELLO');

I am using php to run a server on 8080 port which is serving data to ajax calls.

php -S localhost:8080 -t web web/index.php

Now I want to serve these Ajax calls from using WordPress only. I do not want to run specific php server.


There's little that you can do with the Silex server that can't be done through Wordpress but it takes a bit of effort to get WP to respond to AJAX calls.

The first step is to make the call available through AJAX. This requires adding a line to your functions.php file similar to

add_action('wp_ajax_my_ajax_call', 'onno_update_my_ajax_call');

if this call will be made for guests and customers (i.e. not ADMIN), you'll also need the line.

add_action('wp_ajax_nopriv_my_ajax_call', 'my_ajax_call');

which does the same thing but is more inclusive.

The next step is to create the ajax calls. You don't give an example of such a call so all I can do is advise you to look at the docs for $wpdb. WP has a comprehensive set of calls for retrieving info from the database and for complex queries, you can always use $wpdb->query() which will run arbitrary SQL for you.

The AJAX logic goes in the function my_ajax_call() and the result should be placed into an array or object. The final line of your function should be a call to wp_send_json_success($return) where $return is the object/array of information to be returned.

Using this system, I've been able to add pages to the wp_admin section to allow shop owners to build purchase orders for restocking from WooCommerce data and a side-load gallery for variations (Woo only allows a gallery for the parent).

Here's a quick example:

function my_ajax_call() {
    $return['data'] = date('Y-m-d');

And then in the javascript, more steps are necessary. For one thing, you'll need the WP AJAX URL which is usually /wp-admin/admin-ajax.php but can vary somewhat. It's often made available to Javascript as the global ajaxurl or might be tucked away in another object like woocommerce.ajaxurl. You'll need to construct a Javascript object with an action element that points to your function and any other variables you might need to pass to the AJAX call. For example:

data = {'action':'my_ajax_call'}


data = {'action':'my_ajax_call', 'todo':'getDate'}

        data: data,
        success: function(trn) {$('#data').html(trn.data)}


| improve this answer | |
  • For some further wordpress and ajax research can I suggest having a read of the articles created by Tom McFarlin, he's v.knowledgable about wordpress and has some good posts on ajax specifically [google.co.uk/… – virtualLast Oct 10 '18 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.