Share Article To

We all suffer in our wordpress coding life to pass the PHP variable value to js/jQuery, some time it is working fine and some time not. And if you already went through the files of contributed plugins then you are aware that in most of the plugin in developer are using the SCRIPT tag  in PHP tags to pass the data from PHP to JS. This way work but some time it not.

So what is the way to pass the PHP data to JS?
Wordpress providing us a function to do that and the function is wp_localize_script() using this we can do the same and it is the correct and perfect way to do this.

 Example skeleton:
add_action( 'some_tag', 'your_function' );
function your_function( $parameters ) {
//Do some php job

// localize the data, identifier via script-ID and create object for JS
wp_localize_script( ‘js_file_id’, ‘js_object_name’, $variable );
}

So from next time you are going to share PHP data to JS file using wp_localize_script function – RIGHT?

ENJOY 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *