定制 oberonlai/wp-ajax 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

oberonlai/wp-ajax

Composer 安装命令:

composer require oberonlai/wp-ajax

包简介

Handy tool for WordPress Ajax.

README 文档

README

Simple WordPress Class for ajax which forked from WP_AJAX

Requirements

Installation

Install with composer

Run the following in your terminal to install with Composer.

$ composer require oberonlai/wp-ajax

WP Ajax PSR-4 autoloading and can be used with the Composer's autoloader. Below is a basic example of getting started, though your setup may be different depending on how you are using Composer.

require __DIR__ . '/vendor/autoload.php';

use ODS\Ajax;

class MyAjax extends Ajax { ... }

See Composer's basic usage guide for details on working with Composer and autoloading.

Basic Usage

Below is a basic example of setting up an ajax.

require __DIR__ . '/vendor/autoload.php';

use ODS\Ajax;

Class ExampleAction extends WP_AJAX{

    protected $action = 'example-action';

    protected function run(){

    	// Your Code Here!
    	
    	update_option('name', $this->get('name'));

    }
}
ExampleAction::listen(); // Don't forget this line.

ExampleAction::url() // http://example.com/wp-admin/admin-ajax.php?action=example-action
ExampleAction::url(['name' => 'Anthony Budd']) // http://example.com/wp-admin/admin-ajax.php?action=example-action&name=Anthony%20Budd

Helper Methods

Example::url() // Returns the url of the ajax endpoint. Example http://ajax.local/wp/wp-admin/admin-ajax.php?action=example

$this->isLoggedIn(); // Returns TRUE or FALSE if the current visitor is a logged in user.

$this->has($key); // has() will return TRUE or FALSE if an element exists in the $_REQUEST array with a key of $key

$this->get($key, [ $default = NULL ]); // The get() method will return the specified HTTP request variable. If the variable does not exist it will return NULL by default. If you would like to set a custom string as the default, provide it as the second argument.

$this->requestType(); // Returns 'PUT', 'POST', 'GET', 'DELETE' depending on HTTP request type

$this->requestType('POST'); // Returns (bool) 

$this->requestType(['POST', 'PUT']); // Returns (bool)  

Example

Nonce is very important parameter when using Ajax. Below is a practical example of updating post title via ajax.

use ODS\Asset;
use ODS\Ajax;

Asset::addScript(
	array(
		'name'    => 'my_ajax', // It should be the same with Ajax action name.
		'url'     => ODS_PLUGIN_URL . 'assets/js/ajax.js',
		'deps'    => array( 'jquery' ),
		'version' => ODS_VERSION,
		'ajax'    => true,
		'params'  => array(
			'data1' => 'my_data_1',
			'data2' => 'my_data_2',
		),
	),
);

class MyAjax extends Ajax {
	protected $action = 'my_ajax'; // It should be the same with JS name.
	protected function run() {
		
        $nonce = $this->get( 'nonce' );
        $data1 = $this->get( 'data1' );
        $data2 = $this->get( 'data2' );
		
        // Don't forget verify nonce.
        if ( ! wp_verify_nonce( $nonce, 'my_ajax' ) ) {
			$this->JSONResponse( __( 'Invalid', 'my-plugin' ) );
			exit;
		}
		$url     = wp_get_referer();
		$post_id = url_to_postid( $url );
		$my_post = array(
			'ID'         => $post_id,
			'post_title' => 'This is the 123',
		);
		$book_id = wp_update_post( $my_post );
		$this->JSONResponse( $post_id );
	}
}
MyAjax::listen();

And the JS part:

jQuery(function($){
    $(document).ready(function(){
        var btn = $('#btn')
        btn.on('click',function(){
			var data = {
				action: "my_ajax", // It should be the same with Ajax action name.
				nonce: my_ajax.ajax_nonce,
				data1: my_ajax.data1,
				data2: my_ajax.data2,
			};
			$.ajax({
				url: my_ajax.ajax_url,
				data: data,
				type: 'POST',
				dataType: "json",
				success: function(data){
					alert(data);
					console.log(data);
				},
				error: function(data){
					console.log(data);
				}
			})
        })
    }) 
})

More infomation about class Asset - https://github.com/oberonlai/wp-asset

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固