Sometimes we face “ReferenceError: ajaxurl is not defined” while working with ajax in wordpress. Ajax will not work in this case and we will get this error while checking console by inspecting our browser. Simple solution to resolve this issue is given below.

Solution :

This issue is due to the use of javascript variable to reference the admin-ajax.php file. And the variable “ajaxurl” is not declared for front-end. SO to resolve this issue we need to declare this for front-end by putting below mentioned code at the header.php file of your theme.

<script type="text/javascript">
    var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>

By putting this code will resolve this problem. Enjoy ajax in wordpress 🙂