I'm creating a plugin that at a certain point has to use AJAX to pass information to php depending on the events fired by front end user on the different page.

The event itself is when the user clicks on one of the links.

my ajax code is :

         cache: false,
         timeout: 8000,
         type: 'POST',
         data: {action: 'getpostfilter', year : year, month: month, category: category},
         url: ajax_object_1.ajaxurl,
         success: function(data) {alert( "Handler for .click() called." + category+year+month );},
         error: function() {}


I have created action hooks and the function that ajax calls:

function get_post_filter_by_date_and_category(){

     global $wpdb, $information;

     $information['category']= $_POST['category'];
     $information['year']= $_POST['year'];
     $information['month']= $_POST['month'];

    return $information;


    add_action( 'wp_ajax_nopriv_getpostfilter','get_post_filter_by_date_and_category');

Where global $information was created in the beginning of the plugin source code is used to store the variables that are passed on to the php.

I call the function that supposed to get the post information on the separate page as follows:



        echo('<h1> ELSE</h1>');


I didn't forget to properly enqueue the scripts and I also use wp_localize_script to pass admin-ajax.php as URL to ajax call.

I have absolutely no clue what is going wrong.

I have checked the following using different print and alert functions :

  1. The jQuery I am using is correct and creates all the variables and passes them to AJAX as intended.
  2. AJAX gets all the info ( checked through alert() ) and doesn't throw any errors
  3. PHP code runs without any 'blunt' errors . print_r() is used to check the contents of $information and every time it returns this :Array ( [category] => [year] => [month] => )

I am pretty sure this is not because I am not doing the extra security measures (nonce and etc.)

Please help.

  • The parameter data in your success function in your ajax request is going to contain what's printed out in your ajax php. Remember, this is a separate request to the server, so returning something from the php function won't do anything, you need to echo (or print_r, or whatever) it out because the output of the request is what's going to be returned to you. Also, because it's a separate request, it's not going to modify the global as you desire (it would modify it only for the ajax request itself). What do you get if you console.log(data) in your success function? – Andrew Bartel Jul 29 '14 at 17:25
  • I instead used the alert function to reproduce same results.. I get an aray with all the variables being correct – dev3078 Aug 6 '14 at 15:46
  • Moreover AJAX thinks that it has successively POSTED the data, whereas the php function doesn't detect any related variable in the $_POST (used isset function on $_POST['category'] to check that) – dev3078 Aug 7 '14 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.