logo
Populate the side area with widgets, images, navigation links and whatever else comes to your mind.
18 Northumberland Avenue, London, UK
(+44) 871.075.0336
ouroffice@vangard.com
Follow us

WordPress: Fix ajaxurl Not Defined

WordPress: Fix ajaxurl Not Defined

I came across a situation where I needed to execute some ajax scripts from WordPress front end and I got an error: ajaxurl is not defined. As you may know, ajaxurl is a variable that holds the URL to WordPress ajax processing file, usually in the wp-admin section. In the past I’ve depended on other plugins defining it but in this case it was not. If you come across this issue, all you have to do is declare an action to wp_head and write a function to output the URL. This is also a good idea because you have a variable that may not be easily tempered with by other applications.

add_action('wp_head','f2b_ajaxurl');
function f2b_ajaxurl() {
<script type="text/javascript">
var f2b_ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
}