0

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 ?

5
  • 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? Commented Jan 17, 2013 at 7:14
  • 2
    what action hook do you use to enqueue the scripts? you should be using the wp_enqueue_scripts action on the front end. Commented Jan 17, 2013 at 7:37
  • @Milo wp_head works as well (wp_enqueue_scripts is better) Commented Jan 17, 2013 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, { Commented Jan 17, 2013 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. Commented Jan 17, 2013 at 9:59

1 Answer 1

1

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^^

2
  • This shouldn't make a difference Commented Jan 17, 2013 at 8:48
  • It does make a difference. Commented Mar 5, 2013 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.