plugins - run silex or slim with wordpress

admin2025-06-02  2

I am running a theme on WordPress. In this theme, I am making some AJAX calls where I am expecting some response. (/)

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

index.php

<?php
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');
            });
$app->run();

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.

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

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

index.php

<?php
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');
            });
$app->run();

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.

Share Improve this question edited Jan 11, 2018 at 19:29 rudtek 6,4035 gold badges30 silver badges52 bronze badges asked Aug 26, 2016 at 13:59 Prashant GaurPrashant Gaur 2011 silver badge4 bronze badges 5
  • Seems like more server-side question. If you want allow only requests from your local WordPress installation I suppose you need to configure out your server :) – Nicolas Korobochkin Commented Mar 13, 2017 at 10:28
  • 8 I would highly recommend using the REST API instead of Silex. – kraftner Commented Jul 5, 2017 at 7:36
  • Besides the REST API mentioned by @kraftner, you can also have a look into the Themosis framework. – Fabian Marz Commented Dec 20, 2017 at 9:38
  • For a short example see ie wordpress.stackexchange/questions/301493 For me developer.wordpress/rest-api/extending-the-rest-api/… was very useful. – Clemens Tolboom Commented Apr 25, 2018 at 15:32
  • You could use WP-REST API or you can make custom API using wp_ajax action. here is official doc – idpokute Commented Jul 12, 2018 at 18:04
Add a comment  | 

1 Answer 1

Reset to default 1

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');
    wp_send_json_success($return);
    wp_die();
}

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'}

or

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

(function($){
    $.ajax{
        url:ajaxurl,
        data: data,
        success: function(trn) {$('#data').html(trn.data)}
})(jQuery)

HTH

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748807020a313880.html

最新回复(0)