WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is my code snippet :

wp_enqueue_script( 'ajax-script', get_stylesheet_directory_uri().'/js/myajax.js', array('jquery'), 1.0 );
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

My problem is when ever i use that ajax_object it won't work. Also myajax.js file won't load to the site. But works fine in Admin panel.

Why that enqueue script & localize script not working in front end ?

share|improve this question
If the file is not in the sourcecode of the frontend, please be sure your enqueue-function is not placed in a conditional (like is_admin). Can you confirm this? – fischi Jan 17 '13 at 7:14
what action hook do you use to enqueue the scripts? you should be using the wp_enqueue_scripts action on the front end. – Milo Jan 17 '13 at 7:37
@Milo wp_head works as well (wp_enqueue_scripts is better) – shea Jan 17 '13 at 8:37
Thanks for the comments guys. But actually enqueue script is not at all my problem. I could have added that to the header manually. My problem is Ajax request not working. Getting this error in console. ReferenceError: ajax_object is not defined $.post(ajax_object.ajaxurl, { – Jithesh Kt Jan 17 '13 at 8:46
@JitheshKt - you couldn't add it to the header manually, as your localize call would fail because your 'ajax-script' handle wouldn't exist. so have you used the wp_enqueue_scripts action or not? update your question to show the full code you've used to enqueue and localize. – Milo Jan 17 '13 at 9:59

May be try get_template_directory_uri() instead of get_stylesheet_directory_uri(), if your js file is in your theme, it should work frontend.

little help I know^^

share|improve this answer
This shouldn't make a difference – shea Jan 17 '13 at 8:48
It does make a difference. – kaiser Mar 5 '13 at 15:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.