I ran into this when attempting to integrate Dropbox’s drop in chooser API to a plugin I’m writing.
The API documentation instructs you to place the following script
tag at the top of your file:
<script type="text/javascript" src="https://www.dropbox.com/static/api/1/dropins.js" id="dropboxjs" data-app-key="MY_APP_KEY"></script>
All fine and good, and it actually works when I directly paste it into the page that is called in the admin section. But, I’d like to use some variation of wp_register_script(), wp_enqueue_script() and wp_localize_script() to pass the necessary id and data-app-key.
I’ve tried a couple different variations of this:
add_action('admin_enqueue_scripts', 'add_dropbox_stuff');
function add_dropbox_js() {
wp_register_script('dropbox.js','https://www.dropbox.com/static/api/1/dropins.js');
wp_enqueue_script('dropbox.js');
wp_localize_script('dropbox.js','dropboxdata',array('id'=>"dropboxjs",'data-app-key'=>"MY_APP_KEY"));
}
And:
add_action('admin_enqueue_scripts', 'add_dropbox_stuff');
function add_dropbox_stuff() {
wp_register_script('dropbox.js','https://www.dropbox.com/static/api/1/dropins.js');
wp_enqueue_script('dropbox.js');
wp_localize_script('dropbox.js','dropboxdata',array(array('id'=>"dropboxjs"),array('data-app-key'=>"MY_APP_KEY")));
}
MY_APP_KEY is replaced with the appropriate application key in my code. Would appreciate any direction. Thanks.
EDIT: Also tried to do it with some jquery, but to no avail. Tried it on document load and on document ready. I get a {“error”: “Invalid app_key”} return.
$('script[src="https://www.dropbox.com/static/api/1/dropins.js?ver=3.6"]').attr('id','dropboxjs').attr('data-multiselect','true').attr('data-app-key','MY_APP_KEY');
you can try to use the
script_loader_src
filter hook e.g:Update
i just figured it out myself that the src is escaped by esc_url, so looking a bit more i found the
clean_url
filter which you can use to return the value with your id and app key data :Since WP 4.1.0, a new filter hook is available to achieve this easily:
script_loader_tag
Use it this way:
OK, it seems (to me) that with
wp_enqueque_scripts
is not possible to print the id and the app key as script tag attributes.I’m sure at 90%, because
WP_Dependencies
is not a class that I know well, but looking at the code It seems not possible to me.But I’m sure at 100% that using
wp_localize_script
is not useful for your scope.As I said in my comment above:
What I’ve not said in the comment is that the json-encoded object as an arbitrary name that you decide, in fact, looking at the syntax:
The object named
$object_name
could be used by the script because is in the global scope and printed in the html of the page.But the
$object_name
is a name that you decide, so it can be everything.So ask to yourself:
how the script in the remote dropbox server can make use of a variable that they don’t know how is called?
So there is no reason at all to pass id and/or app key to the script with
wp_localize_script
: you have just to print them as script tag attributes as is said in Dropbox API docs.I’m not a js developer, but I think what dropbox script does is:
<script>
html elements in the pagePlease, note that I don’t know this for sure, I’m just guessing.
In this way, the script loaded from the dropbox server can check your app key in a way that is easy for them and easy to implement for you.
Because in the first sentence I’ve said that is not possible to print the id and the app key in the script using
wp_enqueque_scripts
, moral of the story is that you have to print them in the markup in another way.A way that not smells too much (when there are no alternatives) is to use
wp_print_scripts
hook to print the script tag:From Bainternet’s reply above. This code worked for me.
Edit: The only difference from the Bainternet code is, I added a condition to check if the script URL is dropbox and it is a .js file.
I’m ignoring all the other URL’s and rewriting the dropbox URL.
Thanks for all the postings, they really helped. I did roll my own version to give it some structure and make it easier to read and update. Use enqueue as normal, use script for CSS files with a false tag at the end so that it loads at the top. Though it can probably be simplified somewhat.
I did this with my eCards plugin and it’s really simple.
Here’s a direct copy/paste from the plugin:
Notice the API key is passed via an option.
I did some checking in the dropbox.js code (v2) to see what was happening and how to best solve this. As it turns out, the data-app-key is only used to set the variable Dropbox.appKey. I able to set the variable with the following extra line.
Using the javascript example on the Dropbox page https://www.dropbox.com/developers/dropins/chooser/js:
In my code I set the Dropbox.appKey in every place where I reference the Dropbox javascript routines. Doing this allowed me to use wp_enqueue_script() without the extra parameters.
There’s a simpler way to do this
WordPress syntax for script_loader_tag :
To add any attribute, you can modify your $tag this way :
Which will correctly escape URL.