How to properly use JavaScript in your WordPress plugins. There are two key parts:
1. Use wp_enqueue_script() to load external libraries and standalone scripts, instead of generating your own script header tags.
2. On admin pages, use the admin_print_scripts-[mypage] action hook to register the code on only your plugin’s page, not every admin page.
Both of those techniques have been available since WordPress 2.1, so plugin authors can be pretty confident that there will be no backwards-compatibility issues in employing them. (Despite the fact that WordPress is maintaining the 2.0.x version line until 2010, my research suggests that the number of users of that branch is insignificant.)
- Don’t pollute the global JavaScript namespace, especially with the over-used “$”. A number of JavaScript libraries use the dollar sign in different ways, so you can run into problems if, for example, you’re using jQuery but another plugin has loaded Prototype.
Fortunately, jQuery makes it easy to use “$” in a friendly way with other libraries. My favorite is to wrap jQuery-based code in something like the following, which employs $ outside of the global namespace and waits to execute the script until after the DOM has loaded.
jQuery(function($) { /* some code that uses $ */ });
source: pressedwords
Related Stuff
New WordPress feature, Possibly Related PostsIf you have a blog on WordPress.com, you may have noticed a new feature called Possibly Related that links posts from other blogs in the ...
What Do You Need to Set up a Wordpress BlogI've found myself answering this question a lot lately, so I figure I should just turn it into a blog post so I can point people to ...
How To Make a WordPress Privacy PolicyEver since Adsense updated their terms and conditions, requiring publishers to display a privacy policy on their website(s), webmasters and ...
WordPress Single Post TemplatesAustin recommends using a filter in your functions.php file as an alternative to the method below. IMO, his suggestion is much simpler and ...
Wordpress Plugin - SuperFast Digg ThisSuperFast Digg This is really a super fast social bookmarking plugin, it loads faster than others, scalable and with better performance. It ...
Be the first ... |Add your comment.
Your Comment ...
Name (required)
Email (required, hidden)
Website
