dynamic external javascript generated using php

How to write an external javascript file with dynamic variable values generated using php from wp db values?

So instead of using wp_localize_script for inline js variables, I’d like to use wp_enqueue_script with $src pointing to this php file acting like external js.

So far, on the external js using php, the content as follow:

Header("content-type: application/x-javascript");
$params = array(
  'timezone_string'           => get_option( 'timezone_string' ),
  'jump_to_top'           => __( 'Jump to top' ),
  .....
);
echo "$timezone_string $jump_to_top;
.....

When viewing page source, the external script file is listed correctly, but clicking on the external file link, it takes me to home page. I added wp-load.php, still the same.

How to do this correctly?

1 Answer
1

Rather than generating whole script dynamically it would make more sense (from logical and caching perspective) to use static script that will fetch data required via generic Ajax request to WP.

Leave a Comment