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?
This is the code in widget() function of my WP_Widget class.
wp_register_script('color-script', plugin_dir_url(__FILE__).'scripts/custom.js');
wp_enqueue_script('color-script');
$data = array("text_color" => $instance['text-color'], "bg_color" => $instance['bg-color'], "button_color" => $instance['button-color']);
wp_localize_script('color-script', 'php_data', $data);
This is the javascript in custom.js.
document.getElementById("wid-small-div").style.color = php_data.text_color;
document.getElementById("wid-small-div").style.background = php_data.bg_color;