Php 来自plugin-OOP的WordPress Ajax回调函数
使用来编写插件。 我需要打个ajax电话 我在mainPhp 来自plugin-OOP的WordPress Ajax回调函数,php,ajax,wordpress,oop,Php,Ajax,Wordpress,Oop,使用来编写插件。 我需要打个ajax电话 我在mainplugin.php文件中添加了一个类来注册脚本和处理ajax调用 if ( ! class_exists( 'PoorMansNameSpaceAJAX' ) ) { class PoorMansNameSpaceAJAX{ public static $instance = null; public $nonce = ''; public $name = 'ajaxexample'; public sta
plugin.php
文件中添加了一个类来注册脚本和处理ajax调用
if ( ! class_exists( 'PoorMansNameSpaceAJAX' ) ) {
class PoorMansNameSpaceAJAX{
public static $instance = null;
public $nonce = '';
public $name = 'ajaxexample';
public static function getInstance()
{
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
public function __construct(){
# Could as well be: wp_enqueue_scripts or login_enqueue_scripts
add_action( 'admin_enqueue_scripts', array( $this, 'scriptsEnqueue' ) );
# Logged in users:
add_action( 'wp_loaded', array( $this, 'scriptsRegister' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'scriptsLocalize' ) );
add_action( 'admin_init', array( $this, 'ajaxRegister' ) );
}
public function scriptsRegister( $page ){
$file = 'page-1.js';
wp_register_script(
$this->name ,
WPBP_URLPATH . '/app/files/js/' . $file ,
array('jquery')
);
}
public function scriptsEnqueue( $page ){
wp_enqueue_script( $this->name );
}
public function ajaxRegister() {
add_action( "wp_ajax_{$this->name}_action", array($this, 'ajaxexample'), '1' );
}
public function scriptsLocalize( $page ){
wp_localize_script( $this->name, "{$this->name}Object", array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'action' => "{$this->name}_action"
) );
}
public function ajaxexample(){
ob_clean();
echo json_encode( array(
'success' => true
) );
wp_die();
}
}
}
包括在vendor/autoload.php之后调用的类
主要问题是,尽管脚本已成功注册、排队并本地化,但ajax回调函数没有采取行动
ajax调用返回空响应
( function( $, plugin ) {
$(document).ready( function() {
$('#moreroles').on('click', function(event) {
event.preventDefault();
/* Act on the event */
var data = plugin;
$.ajax({
url: plugin.ajax_url,
data: data,
beforeSend : function( d ) {
console.log( 'Before send', d );
}
})
.done( function( response, textStatus, jqXHR ) {
console.log( 'AJAX done', textStatus, jqXHR, jqXHR.getAllResponseHeaders() );
} )
.fail( function( jqXHR, textStatus, errorThrown ) {
console.log( 'AJAX failed', jqXHR.getAllResponseHeaders(), textStatus, errorThrown );
} )
.then( function( jqXHR, textStatus, errorThrown ) {
console.log( 'AJAX after finished', jqXHR, textStatus, errorThrown );
} );
});
} );
} )( jQuery, ajaxexampleObject || {});
缺少这种用法的正确处理过程。
它做得更好,并为管理员和客户端提供加载程序类。您的
数据
指向包含插件
的变量。什么是插件
?在$中需要类似数据:{'action':'ajax_函数_回调','data':data_to_发送}
。ajax
函数…请查看我的编辑。我将插件称为ajaxexampleObject。在php中,您有add_action(“wp_ajax_u{$this->name}}\u action”、array($this,'ajaxexample')、'1')代码>因此操作是调用isajaxexample
。但是在js中,您有:$.ajax({url:plugin.ajax_url,data:data,…})
,其中data
设置为plugin
,您应该有ajaxexample
作为调用的动作……插件是ajaxxampleobject。看看我最近的编辑。无论如何,这不是问题所在。插件变量正确地从php获取参数。哦,糟糕,没有注意到。在WordPress插件样板中也有类似的问题。无论我如何注册回调函数,都无法加载ajax。最后,我将其从类中删除,并将wp_ajax
hook和回调函数放在插件根文件中。您是在后端还是前端使用此功能?因为前端还需要wp\u ajax\u nopriv
hook。