How to Load Ajax in Wordpress
Personally i prefer to do ajax in wordpress the same way that i would do ajax on any other site. I create a processor php file that handles all my ajax requests and just use that URL. So this is, because of htaccess not exactly possible in wordpress so i do the following.
1.in my htaccess file that lives in my wp-content folder i add this below what's already there
<FilesMatch "forms?\.php$">
Order Allow,Deny
Allow from all
</FilesMatch>
In this case my processor file is called forms.php - you would put this in your wp-content/themes/themeName folder along with all your other files such as header.php footer.php etc... it just lives in your theme root.
2.) In my ajax code i can then use my url like this
$.ajax({
url:'/wp-content/themes/themeName/forms.php',
data:({
someVar: someValue
}),
type: 'POST'
});
obviously you can add in any of your before, success or error type things you'd like ...but yea this is (i believe) the easier way to do it because you avoid all the silliness of telling wordpress in 8 different places what's going to happen and this also let's you avoid doing other things you see people doing where they put js code on the page level so they can dip into php where i prefer to keep my js files separate.
Use wp_localize_script and pass url there:
wp_localize_script( some_handle, 'admin_url', array('ajax_url' => admin_url( 'admin-ajax.php' ) ) );
then inside js, you can call it by
admin_url.ajax_url
Firstly, you should read this page thoroughly http://codex.wordpress.org/AJAX_in_Plugins
Secondly, ajax_script
is not defined so you should change to: url: ajaxurl
. I don't see your function1()
in the above code but you might already define it in other file.
And finally, learn how to debug ajax call using Firebug, network and console tab will be your friends. On the PHP side, print_r()
or var_dump()
will be your friends.
As per your request I have put this in an answer for you.
As Hieu Nguyen suggested in his answer, you can use the ajaxurl javascript variable to reference the admin-ajax.php file. However this variable is not declared on the frontend. It is simple to declare this on the front end, by putting the following in the header.php of your theme.
<script type="text/javascript">
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
As is described in the Wordpress AJAX documentation, you have two different hooks - wp_ajax_(action), and wp_ajax_nopriv_(action). The difference between these is:
- wp_ajax_(action): This is fired if the ajax call is made from inside the admin panel.
- wp_ajax_nopriv_(action): This is fired if the ajax call is made from the front end of the website.
Everything else is described in the documentation linked above. Happy coding!
P.S. Here is an example that should work. (I have not tested)
Front end:
<script type="text/javascript">
jQuery.ajax({
url: ajaxurl,
data: {
action: 'my_action_name'
},
type: 'GET'
});
</script>
Back end:
<?php
function my_ajax_callback_function() {
// Implement ajax function here
}
add_action( 'wp_ajax_my_action_name', 'my_ajax_callback_function' ); // If called from admin panel
add_action( 'wp_ajax_nopriv_my_action_name', 'my_ajax_callback_function' ); // If called from front end
?>
UPDATE Even though this is an old answer, it seems to keep getting thumbs up from people - which is great! I think this may be of use to some people.
WordPress has a function wp_localize_script. This function takes an array of data as the third parameter, intended to be translations, like the following:
var translation = {
success: "Success!",
failure: "Failure!",
error: "Error!",
...
};
So this simply loads an object into the HTML head tag. This can be utilized in the following way:
Backend:
wp_localize_script( 'FrontEndAjax', 'ajax', array(
'url' => admin_url( 'admin-ajax.php' )
) );
The advantage of this method is that it may be used in both themes AND plugins, as you are not hard-coding the ajax URL variable into the theme.
On the front end, the URL is now accessible via ajax.url
, rather than simply ajaxurl
in the previous examples.