Posted By

screamwork on 08/29/10


Tagged

wordpress


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

bronet
mikoli


passing parameter from php to js in wordpress


 / Published in: PHP
 

passing php variables to javascript with build-in function 'wplocalizescript'.

  1. //include js-file static !important
  2. wp_enqueue_script('js-script','/path/to/whatever.js',...);
  3.  
  4. //php variables
  5. $params = array(
  6. 'foo' => 'bar',
  7. 'number' => 123,
  8. );
  9.  
  10. //build in function
  11. wp_localize_script( 'js-script', 'NameForParamsInJava', $params );
  12.  
  13. //in js-code now use
  14. NameForParamsInJava.foo
  15. NameForParamsInJava.number

Report this snippet  

You need to login to post a comment.