Giter Club home page Giter Club logo

wp-ajax-requests's Introduction

WP Ajax Requests

Helper to handle Ajax requests in WordPress.

Features

  • Handles security checks, like user capabilities and nonce verifications.
  • Takes care of the communication with WordPress adding the required action hooks.
  • Has front end support which enqueues scripts, localize and takes care of sending the ajax request.
  • Handles data output with options to choose the format type (json, serialized array...)

Usage

To get it going you need to include the file that will build the final object:

require_once SOME_PATH . 'wp-ajax-requests/request.php';

Then you would create a new instance of WPAjaxRequest new WPAjaxRequest() passing in some arguments. For example:

$args = array(
        'action'     => 'my_action',
        'callback'   => 'my_callback',
        'nonce'      => 'my_nonce',
        'capability' => 'manage_options',
        'output'     => 'Json',
    );

new WPAjaxRequest( $args );

(All arguments explained bellow).

You would have to add your callback function. This can be a function or a method of a class, which you would pass in like: array('MyClass', 'my_method');

function my_callback( $data ) {
    // do stuff
    echo $data;
}

Now you can use the JavaScript helper to send the request. For example, if you are going to send the request on submit a form you onlye need to instantiate a new WPAjaxRequest() with some options, then you would call it's method send() passing the data to be sent as a parameter.

var request = new WPAjaxRequest({
	action: 'my_action',
	nonce: 'my_nonce'
});

myForm.on('submit', function(event){
	event.preventDefault();

	var data = myForm.serialize();
	request.send( data );

});

Arguments

The arguments and it's defaults when calling new WPAjaxRequest( $args );:

 $defaults = array(

        /**
         * Required. The action name to create the custom handler for the wordpress hook.
         * wp_ajax_(action), wp_ajax_nonpriv_(action)
         */
        'action' => '',

        /**
         * Callback function defined by the user to run if the request
         * is valid and passes the security checks.
         */
        'callback' => '',

        /**
         * Action name to create the nonce wp_create_nonce().
         * Security checks will run against this name.
         * If left as null, the nonce verification won't take place.
         */
        'nonce' => null,

        /**
         * Security checks will run against this capability. For example: manage_options
         */
        'capability' => null,

        /**
         * Data output format. Possible values Json, SerializedArra, XML.
         */
        'output' => 'Json',

        /**
         * If set to false, the support for the front end will not be used.
         * The user will need to enqueue scripts and send request manually.
         */
        'use_front' => true,

        /**
         * Name used as a handle for the script in wp_enqueue_script()
         */
        'front_script_handle' => 'wpajaxrequest-script',

        /**
         * You may replace the script responsible to send the request by changing
         * the default path to your own script.
         */
        'front_script_src' => home_url( str_replace( ABSPATH, '', __DIR__ . '/js/wp-ajax-request.js') ),

        /**
         * The name of the variable which will contain the data for wp_localize_script().
         */
        'localize_name' => 'wpar',

    );
);

Options JS

The options and it's defaults for the JavaScript object WPAjaxRequest():

		/**
		 * Required. The action name to create the custom handler for the wordpress hook.
		 * wp_ajax_(action), wp_ajax_nonpriv_(action)
		 */
		action: '',

		/**
		 *Request type. Default POST
		 */
		type: 'POST',

		/**
		 * Data type. Default json
		 */
		dataType: 'json',

		/**
		 * URL to send the request. Default the WordPress admin ajax url.
		 */
		url: ajaxurl,

		/**
		 * Action name to create the nonce wp_create_nonce().
		 * Security checks will run against this name.
		 * Same action name defined before. If not needed leave blank.
		 */
		nonce: '',

		/**
		 * Callback function if success on sending the request.
		 */
		success: this.success,

		/**
		 * Callback function if the request faild.
		 */
		error: this.error

wp-ajax-requests's People

Contributors

lucymtc avatar

Watchers

James Cloos avatar  avatar

wp-ajax-requests's Issues

Fully inject Security Dependency

Currently, the WPAjaxRequest constructor accepts a Security object-parameter, but also accepts args that are used to initialize that Security object. Instead, the Security object that is passed to the WPAjaxRequest constructor should already be initialized with those parameters separately.
i.e.:

$security = new Security( $nonce, $capability );
new WPAjaxRequest( $args, $security, $data_handler);

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.