2013-07-28 54 views
0

在網站的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的「):

<?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.")); 
} 
?> 

<div class="wrap"> 
    <?php screen_icon('options-general'); ?> 
    <h2> 
     <?php _e('Manage Page 2 Settings:'); ?> 
    </h2> 
    <p>Settings form here... 
     <form name="p2_form1" id="p2_form1" method="post" action=""> 
      <?php settings_fields('tp2_opts_groups'); ?> 
     <p>input fields, radio buttons, blah blah</p> 
      <input type="submit" id="test_submit" value="Test Submit Button" class="button-primary" /> 
     </form> 

      <?php 
      // 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) 
      ?> 
      <p> data will appear here...</p> 
      <div id="ajax_data"> 
      <!-- data to eventually appear here! --> 
      </div> 


</div> 

文件的其餘部分應該有人需要看看整個代碼...

插件文件...( '試plugin.php'):

<?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的'):

<?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'):

<?php 

class TestPluginDbSetupCls { 

    function TestPluginDbSetupCls() { 
     global $wpdb; 

     // insert info into DB... 
     // Tested. All code here works. 
    } 

} 
?> 

管理面板主頁...( '叔main.php'):

<?php 
// protect page 
if (!current_user_can('manage_options')) { 
    wp_die (__("You don't have permission to access this page.")); 
} 
?> 

<div class="wrap"> 
    <h2>Test Plugin</h2> 
    <p><b>Manage Page 1:</b></p> 
    <blockquote class="section"> 
     <p><a href="#">Link to page 1 settings</a> -- Here is where you can...</p> 
    </blockquote> 

    <p><b>Manage Page 2:</b></p> 
    <blockquote class="section"> 
     <p><a href="#">Link to page 2 settings</a> -- Here is where you can...</p> 
    </blockquote> 

</div> 

管理面板1 ...( '叔page1.php中'):

<?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.")); 
} 
?> 

<div class="wrap"> 
    <?php screen_icon('options-general'); ?> 
    <h2> 
     <?php _e('Manage Page 1 Settings:'); ?> 
    </h2> 
    <p>Settings form here...</p> 
      <form name="p1_form1" id="p1_form1" method="post" action=""> 
      <?php settings_fields('tp1_opts_groups'); ?> 
      <p>input fields, radio buttons, blah blah</p> 
      <p>&nbsp;</p> 
      </form> 
</div> 
+0

已解決!必須將處理函數(my_action_callback())移動到菜單構建類TestPluginAdminAreaCls內部的TestPluginMenu函數內,還必須將「add_action(wp_ajax_」)移到類函數外部, TestPluginAdminAreaCls。Hope解決方案是對別人有用:) –

回答

0

Ajax調用未被註冊。只好搬到處理函數,my_action_callback(),從文件,'T-使page2.php',到TestPluginMenu()內功能,位於菜單建設類中,TestPluginAdminAreaCls - 都在檔案中,'t-admin.php'

也不得不移動「ADD_ACTION(wp_ajax_ ......」到類的功能之外,TestPluginAdminAreaCls

的教訓:確保你的Ajax處理程序正在在正確註冊第一個地方