Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6945713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:28:18+00:00 2026-05-27T13:28:18+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:28:18+00:00Added an answer on May 27, 2026 at 1:28 pm

    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 );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.