title: Pass php variable to javascript in Wordpress's WP_Widget class
body: <p>In the widget function of my WP_Widget class in my wordpress plugin, I have a php array variable named $hello. I want to pass this variable to javascript. I have seen the function wp_localize_script and tried to use it. But it is not working from inside the function. How to make it work?</p>
  
  <p>This is the code in widget() function of my WP_Widget class.</p>
  
  <pre><code>wp_register_script('color-script', plugin_dir_url(__FILE__).'scripts/custom.js');
  wp_enqueue_script('color-script');
  $data = array("text_color" =&gt; $instance['text-color'], "bg_color" =&gt; $instance['bg-color'], "button_color" =&gt; $instance['button-color']);
  wp_localize_script('color-script', 'php_data', $data);
  </code></pre>
  
  <p>This is the javascript in custom.js.</p>
  
  <pre><code>document.getElementById("wid-small-div").style.color = php_data.text_color;
  document.getElementById("wid-small-div").style.background = php_data.bg_color;
  </code></pre>