Forums

[resolved] Including dynamically generated, locally hosted JavaScript using a plugin (4 posts)

  1. Leirith
    Member
    Posted 7 months ago #

    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?

  2. bcwp
    Member
    Posted 7 months ago #

    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

    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.

  3. Leirith
    Member
    Posted 7 months ago #

    Ah great. Thank you, that worked nicely.

  4. nbhatti2001
    Member
    Posted 4 months ago #

    great helped me too.

Reply

You must log in to post.

About this Topic