Title: Including dynamically generated, locally hosted JavaScript using a plugin
Last modified: August 20, 2016

---

# Including dynamically generated, locally hosted JavaScript using a plugin

 *  Resolved [Leirith](https://wordpress.org/support/users/leirith/)
 * (@leirith)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/including-dynamically-generated-locally-hosted-javascript-using-a-plugin/)
 * Hi,
 * I wish to enqueue a javascript fragment using a plugin, but part of the javascript
   is dynamic, e.g.
 * `myJavaScriptFunction('DYNAMIC_PARAMETER');`
 * I would like to retrieve DYNAMIC_PARAMETER from a plugin setting and insert it
   into the JavaScript function call.
 * I thought I’d be able to use a PHP file to generate the JavaScript file dynamically,
   using the query string to append the wordpress plugin setting, e.g.
 *     ```
       add_action('wp_enqueue_scripts', 'get_js');
       function get_js() {
           $dynamic_parameter = get_option('dynamic_parameter');
           wp_register_script('dynamic_js', plugins_url("/js/dynamic_js.php?dynamicParameter={$dynamicParameter}", __FILE__));
           wp_enqueue_script('dynamic_js');
       }
       ```
   
 * Where js/dynamic_js.php is in my plugin directory and contains the following.
 *     ```
       <?php
       header("content-type: application/x-javascript");
       if (array_key_exists('target', $_REQUEST)) {
           $dynamicParameter = $_REQUEST['dynamicParameter'];
       } else {
           $dynamicParameter = '';
       }
       $script = <<<EOD
       /* <![CDATA[ */
       function myJavaScriptFunction('$dynamicParameter');
       /* ]]> */
       EOD;
   
       echo $script;
       ```
   
 * Unfortunately WordPress says it can’t find the file when I request it. Is there
   a way I can include dynamically generated JavaScript using a WordPress plugin?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [bcwp](https://wordpress.org/support/users/bcwp/)
 * (@bcwp)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/including-dynamically-generated-locally-hosted-javascript-using-a-plugin/#post-2341060)
 * You shouldn’t pass parameters that way. The proper way to pass parameters to 
   JavaScript in WordPress is to use localization. For an example of this, see the
   my **last** post in the following thread:
 * [http://wordpress.org/support/topic/undefined-function-in-javascript-plugin?replies=10](http://wordpress.org/support/topic/undefined-function-in-javascript-plugin?replies=10)
 * In that example, I pass the current WordPress user ID to JavaScript:
 * _snippet from PHP file:_
    … function smp2_enqueue_script() { wp_enqueue_script(‘
   smpayne2’, plugin_dir_url( __FILE__ ) . ‘smpayne2.js’, array( ‘jquery’ ), ‘1.0’);**
   $current_user = wp_get_current_user(); wp_localize_script( ‘smpayne2’, ‘smp2Params’,
   array( ‘userID’ => $current_user->ID ) ); } // smp2_enqueue_script add_action(‘
   wp_print_scripts’, ‘smp2_enqueue_script’ ); …
 * _snippet from JavaScript file (smpayne2.js):_
 * jQuery(document).ready(function($) {
    $(‘a[href^=”mailto:”]’).each(function(){
   var href = $(this).attr(‘href’); href += ( -1 < href.indexOf(‘?’) ? ‘&’ : ‘?’)
   + ‘ul=’ + **smp2Params.userID;** $(this).attr(‘href’, href); }); });
 * You _can_ create dynamic JavaScript files, but again it’s not the recommended
   way. You would enqueue them like this:
    wp_enqueue_script( ‘my-dynamic-script’,
   $path . ‘/myscript.js.php’ ); and don’t include any querystrings in the filename.
 *  Thread Starter [Leirith](https://wordpress.org/support/users/leirith/)
 * (@leirith)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/including-dynamically-generated-locally-hosted-javascript-using-a-plugin/#post-2341312)
 * Ah great. Thank you, that worked nicely.
 *  [nbhatti2001](https://wordpress.org/support/users/nbhatti2001/)
 * (@nbhatti2001)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/including-dynamically-generated-locally-hosted-javascript-using-a-plugin/#post-2341526)
 * great helped me too.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Including dynamically generated, locally hosted JavaScript using a plugin’
is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 3 participants
 * Last reply from: [nbhatti2001](https://wordpress.org/support/users/nbhatti2001/)
 * Last activity: [14 years, 4 months ago](https://wordpress.org/support/topic/including-dynamically-generated-locally-hosted-javascript-using-a-plugin/#post-2341526)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
