$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugins - run silex or slim with wordpress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - run silex or slim with wordpress

matteradmin10PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far