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

I'm creating a plugin that has some external Javascript and some AJAX calls ... but I don't know the best way to make reference to the admin-ajax.php file since the path requires some input from PHP (paths and such).

Should I declare a global javascript variable?

What is the best way to accomplish this?

share|improve this question
up vote 1 down vote accepted

What you need to do is register and/or enqueue your script and then use wp_localize_script to include a Javascript variable on your page. The Codex page has this example:

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 );

You can then access the variable with, to quote the same Codex page:

alert(object_name.some_string); // alerts 'Some string to translate'
share|improve this answer

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.