I have a question.
How can I use Ajax on my templates…
in single.php I have :
$.ajax({ type: "POST", url: "http://www._____wp-content/themes/MS-MangoBerry___/myajax.php", data: "yo", cache: false, success: function(data) { alert("yes"); } });
And in myajax.php, I have
$(document).ready(function() {
alert(“ok”); });
Then I have an error : Fatal error: Call to undefined function get_header() in myajax.php
Why ?
Thanks in advance.
Please also have a look at this article http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/#js-global
It suggests that all AJAX requests should be submitted to /wp-admin/admin-ajax.php
And the you could hook the request by using this code in functions.php
Then you could implement a method in functions.php
On the request you also need to send a parameter name ‘action’ with value of the action name.
in this case it would be action=your_ajax_action_name.
Hope this help 🙂
wordpress has a built ajax url that you need to use. this post will help you out.
http://geekpreneur.blogspot.com/2009/06/how-to-use-wpajax-in-wordpress.html
the tricky thing is how wordpress knows which function will accept your call back. it happens by adding an action. the hook of the action is your ajax action prepended with wp_ajax_