我有主文件爲貝婁:自定義WordPress插件調用阿賈克斯返回0
<?php
/*
Plugin Name: XYZ
*/
class GMS_SMS_Notifier {
public function __construct(){
if (is_admin()){
//JS
add_action('admin_enqueue_scripts',array($this,'load_custom_js_scripts'));
}
}
function load_custom_js_scripts() {
wp_register_script('gms_custom', plugins_url('/assets/js/gms_custom.js', __FILE__),false,'1.0',true);
wp_localize_script('gms_custom','plugin_ajax',array('ajaxurl'=>admin_url('admin-ajax.php')));
wp_enqueue_script('gms_custom');
}
function mail_send_to(){
global $wpdb;
echo $_POST['recordID'].'Hello';
die();
}
add_action('wp_ajax_mail_send_to','mail_send_to');
add_action('wp_ajax_nopriv_mail_send_to','mail_send_to');
} // End Class GMS_SMS_Notifier
new GMS_SMS_Notifier();
和JS文件gms_custom.js
看起來波紋管:
$(".send_mail_btn").click(function(){
var recordids = $(this).attr("id");
$.ajax({
url: plugin_ajax.ajaxurl,
type: 'POST',
data: ({
action: 'mail_send_to',
recordID: recordids,
}),
success: function(data) {
console.log(data);
}
});
});
當我把自定義功能,直接在主題的功能。 PHP的工作正常,但是當把它放在插件文件,而不是工作。我做錯了什麼,我需要改變什麼才能使它從插件工作?
你的權利謝謝:在我的情況下add_action('wp_ajax_mail_send_to',數組($ this,'mail_send_to')); – Ravi