• I’m developing a plugin that embeds widgets from a third-party site via JavaScript. The script requires URL parameters like so:

    <script src="https://somesource/script.js?entityID=12345"></script>

    I know it’s bad to call scripts directly and that I should be using enqueue_script, but is this a good idea in this context? What is the best practice for enqueuing scripts that require URL parameters?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter jasonpomerleau

    (@jasonpomerleau)

    I should add that each widget I embed will require different URL parameters.

    Hello there,

    Follow these links they help you to find the best way to enque a script in a plugin.
    https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
    https://developer.wordpress.org/reference/functions/wp_enqueue_script/

    Hope it helps and let me know if you require any further assistance.

    Moderator bcworkz

    (@bcworkz)

    First of all, there shouldn’t be any problem using wp_enqueue_script() for a script src that includes URL parameters. Just add them on to the src string parameter in the function call. If there is still some problem with enqueuing, (which I can’t imagine) and if there are no dependencies involved and the script element is supposed to be in the head section, you could get away with directly outputting the script element from a hook to “wp_print_scripts” action. This is the action after which enqueued scripts are also output. If it is inappropriate to have your script element be among the first listed, use the “wp_head” hook with a late priority number.

    Thread Starter jasonpomerleau

    (@jasonpomerleau)

    Thanks everyone. The snag here is the the URL parameters are dynamic. I think I’m going to have to use wp_deresgister_script before each wp_enqueue_script for this. If I understand the docs correctly, re-enqueuing ignores any new parameters supplied (including changing the src argument).

    <script src="https://somesource/script.js?entityID=12345"></script>
    <script src="https://somesource/script.js?entityID=67890"></script>

    Moderator bcworkz

    (@bcworkz)

    It seems to me any dynamic element involved can also be used to dynamically define the script src when enqueuing. Assign the current ID to a variable, then concatenate that variable to the wp_enqueue_script() src argument. Just pay attention to variable scope. You may have to declare the variable global or use one of the workarounds used by those that abhor globals.

    Whether re-enqueuing overrides the original arguments or not might have to do with how the original arguments were defined. If the script was first registered, then enqueued, you may be right if you simply tried to re-enqueue. I’ve never tested this behavior. If the script were just enqueued without registering, I think re-enqueuing again without a dequeue would still serve to overwrite the original arguments. Again, I’ve never tested.

    In any case, undoing whatever was done before through deregister and dequeue would certainly allow you to define the current URL parameters.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Enqueuing scripts with URL Parameters?’ is closed to new replies.