Coder Social home page Coder Social logo

ajax's Introduction

Ajax

BracketSpace Micropackage Latest Stable Version PHP from Packagist Total Downloads License

Micropackage logo

๐Ÿงฌ About Ajax

This micropackage is a wrapper for WordPress AJAX responses in PHP.

๐Ÿ’พ Installation

composer require micropackage/ajax

๐Ÿ•น Usage

Basic usage

use Micropackage\Ajax\Response;

function ajax_action_handler() {
	$response = new Response();

	// Handle nonce.
	$response->verify_nonce( $action = 'my_action', $query_arg = 'noncefield', $send_if_failed = true );

	// Do some checks and immediately send an error.
	if ( something_is_wrong() ) {
		$response->error( 'Error message' );
	}

	// This is never reached.
	$response->send( 'All good' );

}

Error collecting

You can collect multiple errors in one response.

use Micropackage\Ajax\Response;

function ajax_action_handler() {
	$response = new Response();

	// Do some checks.
	if ( something_is_wrong() ) {
		$response->add_error( 'Error message' );
	}

	// Do some checks.
	if ( something_else_is_wrong() ) {
		$response->add_error( 'Whoah!' );
	}

	// If no error added, the below message will be sent.
	$response->send( 'All good if no errors' );

}

Sending data

use Micropackage\Ajax\Response;

function ajax_action_handler() {
	$response = new Response();
	$response->send( $data_array );
}

๐Ÿ“ฆ About the Micropackage project

Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.

The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.

Micropackages are maintained by BracketSpace.

๐Ÿ“– Changelog

See the changelog file.

๐Ÿ“ƒ License

GNU General Public License (GPL) v3.0. See the LICENSE file for more information.

ajax's People

Contributors

jakubmikita 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.