Giter Site home page Giter Site logo

ajax-queue's Introduction

AJAX Queue for WordPress

Plugin that allows you to queue AJAX calls and do a single HTTP request.

Activating the plugin will load a global AJAXQueue JavaScript object.

More documentation coming soon (isn't it always?), but this is how it works:

On the JavaScript side

AJAXQueue.add(action, params, callback);

Where:

action is the action registerd with the WP AJAX API (ie: wp_ajax_{action}).

params is the map that represents the actual POST request for this action.

callback is going to be called with the JSON response for this specific action. The exact same callback you use for a standard AJAX call should work fine.

Once you enqueue all your calls, you simply call:

AJAXQueue.run();

On the PHP side

Just register your AJAX handlers as usual. Nothing special. The only requirement is that you use core's JSON functions to return your data: wp_send_json_error and wp_send_json_success. Don't ever use die(); or exit; manually or this won't work. This is a good practice anyway so if this requirement is a problem, shame on you!

Example

PHP

add_action( 'wp_ajax_action_with_error', function () {
	wp_send_json_error( [ 'message' => strtoupper( $_POST['my_arg'] ) ] );
} );

add_action( 'wp_ajax_action_with_ok', function () {
	wp_send_json_success( [ 'message' => md5( rand() ) ] );
} );

JS

AJAXQueue.add('action_with_error', {my_arg:'wooot!'}, function(response){ console.log(response) } );
AJAXQueue.add('action_with_error', {my_arg:'you can call the same action multiple times!'}, function(response){ console.log(response) } );
AJAXQueue.add('action_with_ok', {}, function(response){ console.log(response) } );
AJAXQueue.run();

Result

Demo

ajax-queue's People

Contributors

mzaweb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.