Title: wp_localize_script does not work
Last modified: November 22, 2024

---

# wp_localize_script does not work

 *  [antonop4u](https://wordpress.org/support/users/antonop4u/)
 * (@antonop4u)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/wp_localize_script-does-not-work/)
 * I’m working on a custom plugin to use in one of my websites and I’m having trouble
   with the wp_localize_script word press function, function that I use quite often
   without problems. i create the following function to `enqueue` the scripts and
   styles
 *     ```wp-block-code
       function ar_include_plugin_front_end_files(){	  wp_register_script('ar_home_js', plugin_dir_url( DIR ) . 'public/js/ar-home.js' , array('jquery') , '1.0.0' , true );  wp_register_style('ar_promo_style', plugin_dir_url( DIR ) . 'public/css/ar-promo.css' );  wp_localize_script( 'ar_front_functions_js', 'ar_var' ,array(      'ajaxUrl' => admin_url('admin-ajax.php'),      'nonce' => wp_create_nonce( 'wp_rest' )    )  );  wp_enqueue_style('ar_promo_style');  if( is_front_page() || is_home() ){    wp_enqueue_script('ar_home_js');  }	}add_action( 'wp_enqueue_scripts', 'ar_include_plugin_front_end_files' );
       ```
   
 * the fwo files that I enquee are ok the get inserted in the page code, while wp_localize_scrip
   that should set some variables in the page code to call in javascript just ddo
   nothing. No erreor no variable just like it doesn’t exists.
 * I use the exat same type of code with differnt name in the admin panel and it
   works just fine.
 * Any idea?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/wp_localize_script-does-not-work/#post-18153930)
 * The 1st parameter of [wp_localize_script ](https://developer.wordpress.org/reference/functions/wp_localize_script/)
   should be the handle used by the JavaScript file for which the variables are 
   intended. In your case, this should be `ar_home_js` and not `ar_front_functions_js`.
 *  Thread Starter [antonop4u](https://wordpress.org/support/users/antonop4u/)
 * (@antonop4u)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/wp_localize_script-does-not-work/#post-18153966)
 * I tried ar_home_js, it doen’t work still the same problem. thanks anyway.
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/wp_localize_script-does-not-work/#post-18154099)
 * Just an idea: it could be the wrong order. I would recommend it like this:
 *     ```wp-block-code
       function ar_include_plugin_front_end_files(){	  wp_register_script('ar_home_js', plugin_dir_url( DIR ) . 'public/js/ar-home.js' , array('jquery') , '1.0.0' , true );  wp_register_style('ar_promo_style', plugin_dir_url( DIR ) . 'public/css/ar-promo.css' );    wp_enqueue_style('ar_promo_style');  if( is_front_page() || is_home() ){    wp_enqueue_script('ar_home_js');    wp_localize_script( 'ar_home_js', 'ar_var' ,array(      'ajaxUrl' => admin_url('admin-ajax.php'),      'nonce' => wp_create_nonce( 'wp_rest' )    )  );  }	}add_action( 'wp_enqueue_scripts', 'ar_include_plugin_front_end_files' );
       ```
   
 * Untested.
 *  Thread Starter [antonop4u](https://wordpress.org/support/users/antonop4u/)
 * (@antonop4u)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/wp_localize_script-does-not-work/#post-18166165)
 * yes this way works I get the vaiables I need Thanks
 *  Thread Starter [antonop4u](https://wordpress.org/support/users/antonop4u/)
 * (@antonop4u)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/wp_localize_script-does-not-work/#post-18166200)
 * Now the problem is that the ajax call returns with status code 200 ok, but it
   returns the page I called the ajax call from.
 * I’ve been using this functions for a while now and it always work, now it simply
   doesn’t.
 * this is on the server side:
 *     ```wp-block-code
       add_action( 'wp_ajax_ar_ajax_click_count', 'ar_ajax_click_count' );add_action( 'wp_ajax_nopriv_ar_ajax_click_count', 'ar_ajax_click_count' );function ar_ajax_click_count(){	if ( wp_verify_nonce( $_POST['_wpnonce'], 'wp_rest' ) ){		if ( preg_match( '/^(\d){1,10}$/' , $_POST['id'] ) ) {      // if (!current_user_can('administrator')) {        count_clicks(  $_POST['id'] );      // }			echo json_encode(        array(		'sts' => $_POST['id']        )			);			exit;		} else {			exit;		}	} else {		exit;	}}
       ```
   
 * This is on the javascript side:
 *     ```wp-block-code
       function javascripCountCliks(id){	jQuery(document).ready( function($) {		$.ajax( {			method : 'POST',			dataType : 'json',			url : ar_var.ajaxurl,			data : {				id : id,				_wpnonce : ar_var.nonce,				action : 'ar_ajax_click_count' },			beforeSend : function( xhr ){			}		} )		.done(			function( data ){				console.log(data);			}		);	} );}
       ```
   
 * any Idea?
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/wp_localize_script-does-not-work/#post-18166281)
 * Do not use `wp_verify_nonce` for the nonce check but rather [https://developer.wordpress.org/reference/functions/check_ajax_referer/](https://developer.wordpress.org/reference/functions/check_ajax_referer/)
 *  Thread Starter [antonop4u](https://wordpress.org/support/users/antonop4u/)
 * (@antonop4u)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/wp_localize_script-does-not-work/#post-18168189)
 * Thanks, Now it works.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘wp_localize_script does not work’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 7 replies
 * 2 participants
 * Last reply from: [antonop4u](https://wordpress.org/support/users/antonop4u/)
 * Last activity: [1 year, 6 months ago](https://wordpress.org/support/topic/wp_localize_script-does-not-work/#post-18168189)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
