简体   繁体   中英

wordpress admin-ajax.php issues

I'm working on a wordpress project and having a frustrating issue with ajax.

I'm trying to set up an ajax call from a wordpress page to a php script I have called getHistoricalTransFunctions.php. getHistoricalTransFunctions.php then includes a file full of functions, and runs the function I need. The needed function then prints out a response, which is sent back to my javascript code which then displays the response. The catch is, the function I'm trying to call NEEDS to be in the wordpress environment because it calls specific wordpress functions.

I did some research and found that wordpress provides an ajax handler in admin-ajax.php. I've followed a number of tutorials, including:

http://codex.wordpress.org/AJAX_in_Plugins/

http://www.1stwebdesigner.com/css/implement-ajax-wordpress-themes/

http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/

I've followed all these, but still for whatever reason I'm getting a "-1" response from the admin-ajax.php page. I traced it and found it originates from the is_user_logged_in() function. Apparently wordpress doesn't think my user is logged in, so it errors out in that code block. Here is some of my code:

This is my javascript call:

$('button#RunReportButton2').click(function() {
  $('#transactionContainer2').html("<img src='<?php echo $RootDomain; ?>/wp-content/themes/test-client/images/ajax-loader.gif' id='ajaxloader' style='margin: 170px auto auto 340px;' />");
  var fromDate2 = $('#fromDate2').val();
  var toDate2 = $('#toDate2').val();
  $.ajax({ type: "POST",
    url:ajaxurl,
    type:'POST',
    data: { action:"runReport2",
            startingInt:"0",
            fromDate:fromDate2,
            toDate:toDate2 },
    success: function(html) {
      $('#transactionContainer2').html(html);
    }
  });
  return false;
});

I added this to the bottom of admin-ajax.php:

add_action(wp_ajax_nopriv_runReport2, runReport2);
add_action(wp_ajax_runReport2, runReport2);

Then my actual php function that is called is:

function runReport2() {
  include("$RootDomain/wp-content/themes/test-client/reports/historicalTransFunctions.php");

  $startingIndex = $_POST['startingInt'];
  //$startingIndex = 0;
  $fromDate = $_POST['fromDate'];
  //$fromDate = "2011-02-11";
  $toDate = $_POST['toDate'];
  //$toDate = "2011-12-05";

  // post variable sanitization
  if(!is_numeric($startingIndex)) {
    printHistoricalTransactions($token, $client, 0);
    die();
  }

  if($startingIndex <= 0) {
    printHistoricalTransactions($token, $client, 0);
    die();
  }

  // match date
  $dateregex = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/';

  if($toDate != "" && $fromDate != "") {
    if(preg_match($dateregex, $fromDate) && preg_match($dateregex, $toDate))
      printHistoricalTransactions($token, $client, $startingIndex, $fromDate, $toDate);
  } else {
    printHistoricalTransactions($token, $client, $startingIndex);
  }
  die();
}

I'm wondering if admin-ajax.php is the best method of doing what I need to do, and I'm also wondering why this isn't working? Thanks!

First thing you should never modify core files. There is no need to add your ajax function to admin-ajax.php. Just put the add_action right above the function. Also your add_action is missing quotation marks around the action and function names (probably an oversight when posting your code here).

Also the javascript ajaxurl variable is not available on the front end unless a user is logged in. You need to define the variable in your js: ajaxurl = 'http://your_site.com/wp-admin/admin-ajax.php';

Often with admin-ajax.php I have to use output buffering to get it to work right:

 function runReport2() {
 ob_start ();
 //your code

 //end your code
$response = ob_get_contents ();
    ob_end_clean ();
    echo $response;
    die( 1 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM