passing parameter from php to js in wordpress


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.