Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

sinious on "Best practice means of "waiting" until specific JavaScript is loaded in Plugin"

$
0
0

Hey all,

Brand new to WP. I've been toying with creating a plugin and I'd like to use jQuery UI. I'm finding that a <script/> I add directly into some content loaded by an admin menu is firing off too fast. jQuery is loaded but not jQuery UI.

Since jQuery UI is only needed in the plugin administration pages, I'm trying to keep from unnecessarily loading it otherwise. I put it in my /myplugin/lib/jquery.ui.min.js folder and upon launching the menu item which triggers the page content to load, I run wp_enqueue_script() in that function right before loading content.

e.g.:

# ...
# add the submenu link to the function
add_submenu_page( 'parent-menu-id', 'The Page', 'The Page', 'manage_options', 'my-page-unique-id', 'my_plugin_some_panel'); 

# ... 

function my_plugin_some_panel() {
	wp_enqueue_style("jquismoothness", plugins_url("css/smoothness/jquery-ui-1.10.4.custom.min.css", __FILE__), FALSE);
	wp_enqueue_script("jquery_ui_ldr", plugins_url("js/jquery-ui-1.10.4.custom.min.js", __FILE__), FALSE);
	include(plugin_dir_path(__FILE__) . '/views/thismenuspage.php');
}

The menu/submenu shows up fine, the page shows up fine but inspecting the JavaScript console (or just <script>console.log(jQuery.ui);</script>) shows up as undefined. So the queued script to load isn't quite ready yet.

I feel I'm way off course on how I should be doing this. My only course of action here is to literally setTimeout() and run a loop continuously sniffing for jQuery.ui to be defined before proceeding with the page. It works but it definitely feels dirty.

What is the best way to assure this library loads before I start to make use of it in the plugin admin panels?

Should I be putting my JS in the /js/ folder with yet another wp_enqueue_script() call to it with jquery UI as a dependency?

Thanks for tips!


Viewing all articles
Browse latest Browse all 8245

Trending Articles