WordPress add ajax url variable

1.96K views
no comments
22 Apr 2015 2:29 am

we have need a Ajax url in wordpress that not available already available in our theme pages, so we add it through a WordPress Hook  API, so lets add this code on your theme functions.php file.

Important: you must call wordpress function

<?php wp_head(); ?> 

in your header.php file or if you don’t have header.php then call it index.php or front-page.php or home.php between <head></head> TAG, if you don’t know how to set up wordpress pages files you must read WordPress template hierarchy structure.

<?php
//ADD_ACTION IS HOOK
 add_action('wp_head','wp_ajax_url');

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

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>