Solution to ReferenceError: ajaxurl is not defined
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 🙂
Thanks a lot!! It solved my problem
Llevo horas buscando la solución a este problema, he estado buscando por miles de foros y webs de internet y no daba con la solución. Y esto ha arreglado el problema al segundo. Muchas gracias!!!
Thanks a looooooooooot
THank you!