在网站的Subdomain上构建一个wordpress插件(我只提到它,因为我不认为这是一个xss问题)。我在管理方面使用Ajax和Jquery来实现功能(后端,而不是前端);在管理面板上。
但是,Ajax一直返回0;尽管我在调用/处理函数结束时使用了'die()',并且使用了正确的Ajax'Action'调用(wp_ajax_ACTION)。下面的完整代码。希望你能帮助。
jQuery的...( 'T-admin.js'):
jQuery(document).ready(function($) {
$('#p2_form1').submit(function() {
var data = {
action: "results_test"
};
try {
$.post(ajaxurl, data, function(response) {
// jQuery('#ajax_data').html(response);
alert ('Response is: ' + response);
// this call should return "DATA TO BE ECHOED!"
// alert (typeof response); // this returns 'string'; don't know why
});
} catch (err) {
return err;
}
return false;
});
});
PHP的呼叫/处理程序文件(管理面板第2页)......(“T-第2页。 PHP的“):
// protect page; make sure user can update opts
if (!current_user_can('manage_options')) {
wp_die (__("You don't have permission to access this page."));
}
?>
Settings form here...
input fields, radio buttons, blah blah
// error_reporting(E_ALL);
// TESTING AJAX RESUTLS...
function my_action_callback() {
echo ("DATA TO BE ECHOED!");
die();
}
// Keep getting 0. Why? Am using "die()" at end and wp_ajax_ACTION is correct.
add_action('wp_ajax_results_test', 'my_action_callback');
// add_action('wp_ajax_nopriv_results_test', 'my_action_callback');
// NoPriv not necessary. This is on Admin Panels (Backend, NOT Frontend)
?>
data will appear here...
文件的其余部分应该有人需要看看整个代码...
插件文件...( '试plugin.php'):
defined('ABSPATH') OR exit;
/*
Plugin Name: Test Plugin
Version: 1.0
Author: WP Plugin Newbie
Description: Building my first plugin.
Version: 1.0
License: Free
*/
// protect page
if (!function_exists('add_action')) {
echo ("Sorry, this page doesn't do much when accessed directly");
exit(0);
}
// main class to handle function calls
class TestPluginCls {
public $version_num = '1.0';
function TestPluginCls() {
// get constants
$this->TestPluginConstants();
// register db setup
register_activation_hook(__FILE__, array(&$this, 'setup_DB'));
// action
add_action('plugins_loaded', array(&$this, 'start_TestPlugin'));
}
// set up db
function setup_DB() {
// require
require_once (dirname(__FILE__) . '/admin/t-dbsetup.php');
// call db class
$this->TestPluginDbSetupCls = new TestPluginDbSetupCls();
}
// launch Test Plugin
function start_TestPlugin() {
if (is_admin()) {
// require
require_once (dirname(__FILE__) . '/admin/t-admin.php');
// setup Admin area
$this->TestPluginAdminAreaCls = new TestPluginAdminAreaCls();
}
}
// define constants
function TestPluginConstants() {
define ('TestPlugin_FOLDER', plugin_basename(dirname(__FILE__)));
define ('TestPlugin_URL', plugin_dir_url(__FILE__));
define ('TestPlugin_PATH', plugin_dir_path(__FILE__));
}
}
global $TestPlugin;
global $wpdb;
$TestPlugin = new TestPluginCls();
?>
管理员/菜单建筑类...( 'T-admin.php的'):
error_reporting(E_ALL);
// ini_set('display_errors', '1');
class TestPluginAdminAreaCls {
public $role = 'activate_plugins';
function TestPluginAdminAreaCls() {
// register stuff
add_action('admin_menu', array(&$this, 'TestPluginMenu'));
add_action('admin_init', array(&$this, 'register_tplugin_options'));
// add_action('wp_ajax_get_tmps', 'my_action_cb'); // placing here produces error notice!
// add_action('admin_enqueue_scripts', array(&$this, 'tp_load_admin_scripts'));
}
// build menu
function TestPluginMenu() {
$phsfx_home = add_menu_page(__('Test Plugin Admin Area'), __('Test Plugin'), $this->role, TestPlugin_FOLDER, array(&$this, 'output_page'));
$phsfx_main = add_submenu_page(TestPlugin_FOLDER, __('Test Plugin Admin Area'), __('TP Main'), $this->role, TestPlugin_FOLDER, array(&$this, 'output_page'));
$phsfx_p1 = add_submenu_page(TestPlugin_FOLDER, __('Test Plugin : Page 1'), __('TP Page 1'), $this->role, 'tp1', array(&$this, 'output_page'));
$phsfx_p2 = add_submenu_page(TestPlugin_FOLDER, __('Test Plugin: Page 2'), __('TP Page 2'), $this->role, 'tp2', array(&$this, 'output_page'));
// only show admin scripts and dependencies on TP pages as needed
add_action('admin_print_scripts-' . $phsfx_home, array(&$this, 'tp_load_admin_scripts'));
add_action('admin_print_scripts-' . $phsfx_main, array(&$this, 'tp_load_admin_scripts'));
add_action('admin_print_scripts-' . $phsfx_p1, array(&$this, 'tp_load_admin_scripts'));
add_action('admin_print_scripts-' . $phsfx_p2, array(&$this, 'tp_load_admin_scripts'));
}
function register_tplugin_options() {
// add_action('wp_ajax_get_tmps', 'my_action_cb'); // placing here throws crazy error notice!
register_setting('tp1_opts_groups', 'tp1_opts');
register_setting('tp2_opts_groups', 'tp2_opts');
}
// output proper page
function output_page() {
switch ($_GET['page']) {
case "tp1" :
include_once(dirname(__FILE__) . '/t-page1.php');
break;
case "tp2" :
include_once(dirname(__FILE__) . '/t-page2.php');
break;
default :
include_once(dirname(__FILE__) . '/t-main.php');
break;
}
}
// scripts/css
function tp_load_admin_scripts() {
// load JS
wp_enqueue_script (array('jquery', 'farbtastic', 'media-upload', 'postbox', 'thickbox'));
wp_enqueue_script ('t-admin-js', TestPlugin_URL.'js/t-admin.js', array('jquery'), '1.0');
// localize???
//wp_localize_script ('t-admin-js', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php'))); // don't know if necessary
// load CSS
wp_enqueue_style (array('thickbox', 'farbtastic'));
wp_enqueue_style ('t-admin-css', TestPlugin_URL.'css/t-admin.css', array(), '1.0', 'screen');
}
}
?>
DB设置类...( '叔dbsetup.php'):
class TestPluginDbSetupCls {
function TestPluginDbSetupCls() {
global $wpdb;
// insert info into DB...
// Tested. All code here works.
}
}
?>
管理面板主页...( '叔main.php'):
// protect page
if (!current_user_can('manage_options')) {
wp_die (__("You don't have permission to access this page."));
}
?>
Test Plugin
Manage Page 1:
Link to page 1 settings -- Here is where you can...
Manage Page 2:
Link to page 2 settings -- Here is where you can...
管理面板1 ...( '叔page1.php中'):
// protect page; make sure user can update opts
if (!current_user_can('manage_options')) {
wp_die (__("You don't have permission to access this page."));
}
?>
Settings form here...
input fields, radio buttons, blah blah
2013-07-28
C King
+0
已解决!必须将处理函数(my_action_callback())移动到菜单构建类TestPluginAdminAreaCls内部的TestPluginMenu函数内,还必须将“add_action(wp_ajax_”)移到类函数外部, TestPluginAdminAreaCls。Hope解决方案是对别人有用:) –