I need to pass the path to a php file (located in a folder in my theme) to a JS file being used in my admin area but I have no idea how to accomplish that.

What Im trying to do, is build a page where selecting some options in a form will pass the input's value to a php script via ajax. But because JS is client side, I dont know how to point to the correct location.

I tried testing using this:

  wp_enqueue_script( 'some_handle' );
    $translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' );
    wp_localize_script( 'some_handle', 'object_name', $translation_array );
    wp_enqueue_script("admin", $adminDir."js/admin.js", false, "1.0");   

with this in my admin.js:


But console just reads:

ReferenceError: object_name is not defined

2 Answers 2


If you want to use the variables from the admin.js script, and you want to be sure it works you have to

  1. use as handle for wp_localize_script the handle of the script that have to use data
  2. call wp_localize_script after that script is enqueued

    wp_enqueue_script( 'some_handle' );
    wp_enqueue_script("admin", $adminDir."js/admin.js", false, "1.0"); 
    $translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' );
    wp_localize_script( 'admin', 'object_name', $translation_array );

Using the code above and console.log(object_name.some_string); you will see the translated string on your console.

Read more on wp_localize_script on Codex.


You can first register the script and enqueue it after wp_localize_script.

wp_register_script( 'script_handle', $adminDir."js/admin.js", false, '1.0' ); //register script

$translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' );
wp_localize_script( 'script_handle', 'object_name', $translation_array );
wp_enqueue_script("script_handle"); //enqueue

Function Reference: wp_localize_script

  • Why register -> localize -> enqueue when you can just enqueue -> localize? And what's the additional value of this answer toward the mine posted 3 hours before?
    – gmazzap
    Oct 5, 2013 at 5:05
  • @G.M. There might not be additional value but i have had a similar problem some weeks before and registering the script before en-queuing it fixed it. I don't know the reason though.
    – Konsole
    Oct 5, 2013 at 5:53
  • Im still getting the console error I was getting before. I just copied and pasted exactly was was suggested (in both answers) and Im getting the same error :/
    – rugbert
    Oct 5, 2013 at 13:27
  • ah ok I got it I had to set the wp_localize_script handle to the same handle as the admin.js handle
    – rugbert
    Oct 5, 2013 at 14:07
  • Yes, you're right @rugbert , I just edited this in the answer to make sure others copy/paste it correctly
    – Aurovrata
    Jan 14, 2017 at 8:58

Your Answer

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

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