Sending data from php to javacript in WordPress

If you find this free website useful – why don’t you support this with a donation? It is easy…. read more ….

It is often useful to send data from PHP to Javascript and WordPress gives you a method.

You can use the localise script function to send data from php to javascript in WordPress coding


$params = array (
"imageurl" => plugins_url('images/', __FILE__),
"post_id" => get_the_id(),
wp_localize_script( 'myScript', 'myparams', $params );

and access it in your javascript thus  e.g.


The localisation needs to be called after the script is enqueued. This is useful method to pass static option data through.
However it doesn’t work if you need data from the wordpress loop after the </head> tag is closed
so you need to use this style

<script type="text/javascript">
myparams=new Object();
myparams.numbers = <?php echo "'".$number_of_items."'"; ?>;


Leave a Reply

Your email address will not be published.