I am trying to pass data from an ajax call to a specific page of WordPress.
Below is the code I am using:
jQuery.ajax({type: "POST",
url: 'single-member-page.php',
data:{ size: 'item' },
success: function(){
//alert(data);
jQuery('#display-member-size').text(data);
}
});
The script does not work for WP. I also inspected the page using the console and I get an error:
single-member-page.php” NOT FOUND
I am new to WP and I do not know how to pass data from an ajax call to a specific page.
@Daniel
You asked a very good question, before proceeding to solution we need to understand thumb rules of wordpress ajax.
Thumb Rules:
According to wordpress standards all the ajax request should come to “ajaxurl” in javascript. It actually contains the “wp-admin/admin-ajax.php” file path.
Example:
If you are doing some sutff in wp-admin dashboard section or related to wp-admin section like creating an option page in wp-admin dashboard area, than “ajaxurl” global variable will be always available there in javascript.
If case of this ajax request is initialized from front end page/post than you have to specify admin-ajax.php files path using following method and better if you localize this for front-end javascript,So it will be available in javascript variable like it is available in wp-admin dashboard section.
In order to achieve this we need to add few more lines of code.
Method:
Updated Example code front-end ajax call :
After this we need to write method to handle this ajax request and send output back to the ajax call.
For detecting ajax request call in wordpress they have two standard hooks, hooks are just events like when i send a ajax request, wordpress ajax related hook will trigger and i can call any function on that trigger.
So basically for handling ajax request below are two hooks:
(Used for front-end page/post ajax call related tasks )
Here (action) is the name of the method that you have to code in your current theme function.php file, and this method will handle this ajax request.
Examples:
Object oriented style:
Note: Here “cleanResponseFiles” is Method of class that will handle ajax request. and $this is pointing current class object.
Procedural style:
Note: Here “cleanResponseFiles” is function added in current theme function.php file and it will handle ajax request.
In this we are considering that ajax request can be made either from wp-admin dashboard or from front-end page/post.
Example ajax request handler method:
Thumb Rule :
It’s just for avoding CRSF (Cross site request forgery ) by adding Honeypot , A hidden input field with generated random key and at in request handler method it should be validated.
These are the methods that we can use for creating Wordepress nonce and verifying WordPress nonce and ajax request handler or simple http request handler.
WordPress Nonce Methods:
I will attach a working example of wordpress ajax call ASAP in this comment.
Just a brief: All ajax post should be sent to admin-ajax.php Each request needs to supply at least one piece of data called action. Based on this action, the code in admin-ajax.php creates two hooks.
if the value of action is cusom_action, wp_ajax_custom_action and wp_ajax_nopriv_custom_ction are auto created. Check WordPress coddex. https://codex.wordpress.org/AJAX_in_Plugins
Refer this https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)
Basically you create an action like
wp_ajax_myaction
, this you can define in yourfunctions.php
or somewhere where you seem fit. And then call it as shown in the example(Usage section) on the page.EDIT:
Adding some code to help you understand
In your
functions.php
In your JS:
Few things about the code:
This is just a quick and dirty code, mostly ripped off from the example just to show you how it will work.
The no_priv action is used for allowing unauthorized access(i.e to non-admin users as well)
The
url
is usually not hardcoded in the way shown in the example, people usually pass it to the script usingadmin_url( 'admin-ajax.php' )
The
action
, sent in the data determines which function should be called. (my_ajax
in our case)Let know if you still have issues.