$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>theme development - How to Globally Use wp_localize_script() Ajax URL|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

theme development - How to Globally Use wp_localize_script() Ajax URL

matteradmin6PV0评论

I have added this to my functions.php and need to use ajaxURL in all of enqueued scripts in the template (instead of enqueuing only one script here

add_action( 'wp_enqueue_scripts', 'ajaxify_enqueue_scripts' );
function ajaxify_enqueue_scripts() {
   wp_localize_script( 'ajaxify', 'ajaxURL', array('ajax_url' => get_template_directory_uri() . '/app/login.php' ));
}
add_action( 'wp_ajax_nopriv_set_ajaxify', 'set_ajaxify' );
add_action( 'wp_ajax_set_ajaxify', 'set_ajaxify' );

but when I try to call an ajax method I am getting this error

Uncaught ReferenceError: ajaxURL is not defined

Is there any way to add the ajaxURL to all scripts?

I have added this to my functions.php and need to use ajaxURL in all of enqueued scripts in the template (instead of enqueuing only one script here

add_action( 'wp_enqueue_scripts', 'ajaxify_enqueue_scripts' );
function ajaxify_enqueue_scripts() {
   wp_localize_script( 'ajaxify', 'ajaxURL', array('ajax_url' => get_template_directory_uri() . '/app/login.php' ));
}
add_action( 'wp_ajax_nopriv_set_ajaxify', 'set_ajaxify' );
add_action( 'wp_ajax_set_ajaxify', 'set_ajaxify' );

but when I try to call an ajax method I am getting this error

Uncaught ReferenceError: ajaxURL is not defined

Is there any way to add the ajaxURL to all scripts?

Share Improve this question edited Nov 16, 2018 at 4:47 Behseini asked Nov 16, 2018 at 4:29 BehseiniBehseini 5352 gold badges9 silver badges25 bronze badges 2
  • What action is ajaxify_enqueue_scripts hooked to? – Milo Commented Nov 16, 2018 at 4:40
  • Hi Milo sorry I just updated the code (forget to add the action). As you can see it is hooked to 'wp_enqueue_scripts' – Behseini Commented Nov 16, 2018 at 4:48
Add a comment  | 

2 Answers 2

Reset to default 2

You can conditionally echo the code on only few templates or specific pages. Here is an example:

add_action ( 'wp_head', 'my_js_variables' );
function my_js_variables(){
  // for specific page templates
  $current_template =  get_page_template();

  // return if there is no page template, or if the page template is other than template-x1.php or template-x2.php
  if( !isset($current_template) || ( $current_template != 'template-x1.php' && $current_template != 'template-x2.php' ) ){ return; } ?>
  <script type="text/javascript">
    var ajaxurl = <?php echo json_encode( admin_url( "admin-ajax.php" ) ); ?>; 
    var ajaxnonce = <?php echo json_encode( wp_create_nonce( "itr_ajax_nonce" ) ); ?>;
    var myarray = <?php echo json_encode( array(
         'foo' => 'bar',
         'available' => TRUE,
         'ship' => array( 1, 2, 3, ),
       ) ); ?>
  </script>
<?php
}

To have the ajaxurl variable available on the frontend the easiest way is to add this snippet to you theme’s function.php file:

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

This get the url of the ajax submission page and creates a variable in the head of the HTML with it. Now ‘ajaxurl’ is available in your theme so you can start making it more modern and dynamic.

Post a comment

comment list (0)

  1. No comments so far