Accessing $_POST data in functions.php from a template file after it's been sent via ajax

I am able to retrieve data sent via ajax in functions.php. But I need that data in template.php (of my theme). This is my first time using ajax and maybe I'm going about it the wrong way. I am able to echo $_POST['myvar']; within the functions.php (I will be posting the code once back to work). Assuming the setup is correct, can I access the ajax data outside of the functions.php? Btw, I just signed up here at Stack as well, so if failed to follow some procedure, I apologize.


Thanks guys - here is a sample code. In the js file I have:

$(window).load(function(){ $("#cat").on("click",function() { var selectedCat = $(this).children("option").filter(":selected").text(); $.get('../../../../../../wp-admin/admin-ajax.php', { action: "parent_cat_send", parent_cat: selectedCat }); }); });

And in functions.php I have:

add_action('wp_ajax_parent_cat_send', 'current_par_cat'); add_action('wp_ajax_nopriv_parent_cat_send', 'current_par_cat'); function current_par_cat() { global $parent_cat; $parent_cat = $_GET['parent_cat']; echo $parent_cat; wp_die(); }

The response has the $parent_cat value. I would like to use $parent_cat outside of functions.php i.e in the template.php. So far, I've tried creating a function with global variable:

function set_global_var($new_value) { global $my_global_var; $my_global_var = $new_value; }

and calling it inside the ajax function:


To further explain my goal, I'm trying to get the selected value from a drop-down. This value is the parent category name generated by wp_dropdown_categories. After getting it, I would like to then get that parent category's sub categories. I thought of Ajax (which I've never used before) because I'd like to have two drop-down options - one with the parent categories, and the other with subcategories generated by the selected parent category(possibly without refreshing the page). Thanks.

-------------Problems Reply------------

Follow the step to rendor your data in you template file. There are two way to rendor data that you can implement same time but its denpands on the situation.

  1. You are sending ajax request from admin panel and want to display on frontend
  2. You are sending ajax request form front end and want to display on frontend

If you are sending ajax request from admin panel and want to update the template(front end) in this case first you have to stroe the your data in database. As you said you accessed the data through functions.php file now you have to update_option() to add or update your data to wordpress database. Add have to check and fetch it in your template with get_option() But it will only rendor on page refresh from front end.

Now situation two, You are sending data from front end and want to update it also. in this case follow the first step to update_option() and get_option() and also send response from functions.php that you want to display in your front page(Only on the same page that you used to send request.) and and display it. So that if you refresh page then the same updated data will rendor on your template.

Hope you understand what i tried to explain.

maksbd19 gave me the right answer as I can't access the variable outside of functions.php. Not sure how to mark the response as the correct answer but here it is:

"agreed with @rjdown it is impossible to assist you with this little information. But what I can say for now is as far as I know you can not use your ajax code in your theme file/s as they are not loaded when your ajax request is parsed. Only the functions.php file is loaded and you should have all of your business logic in this file only. – maksbd19 Apr 26 at 3:28"

Category:php Views:4 Time:2018-03-11

Related post

Copyright (C), All Rights Reserved.

processed in 0.141 (s). 11 q(s)