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');
wp_localize_script
do is print a json-encoded object in the html output of the page. This object is recognized by the script and so you can use it. What you need is to add some attributes to script tag, and sowp_localize_script
can't help you.wp_localize_script
doesn't create script attributes. But is it possible to pass the app key directly into dropbox.js? Just a guess but have you triedarray('appKey'=>"MY_APP_KEY")
? This is the code that grabs the key from the attributeif(!Dropbox.appKey){Dropbox.appKey=(e=document.getElementById("dropboxjs"))!=null?e.getAttribute("data-app-key"):void 0}
wp_localize_script
sure you can pass attributes to the script. I really don't know if this will work or not, however it is not a worpress related matter.