Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress Plugin Development - How to use JQuery / JavaScript?

Just started developing a plugin for WordPress, wanted to use some JQuery in the Plugin Admin interface.

How do I properly include and call JQuery?

For instance, on a normal HTML page I would just include the JQuery library, then call this script:

$(document).ready(function(){
    alert('Hello World!');
});

How can I do this in a WordPress Plugin PHP file?

like image 364
JimmyJammed Avatar asked May 29 '13 20:05

JimmyJammed


People also ask

Can I make WordPress Plugin with JavaScript?

Show activity on this post. The answer is no, you cannot, as you have referenced in your question. By definition, a plugin is at the very least a "PHP file with a WordPress plugin header comment" (Wordpress Docs). However, there is no reason a Wordpress plugin can't be primarily Javascript.

Does jQuery work on WordPress?

Enqueue jQueryYou can enqueue any jQuery script that is included in WordPress, or add your own jQuery scripts to WordPress. In WordPress, scripts are loaded with the function wp_enqueue_script(), which is placed in the functions. php of the active WordPress theme. To enqueue jQuery, add this code to the functions.

Can jQuery be used in JavaScript?

jQuery is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery. jQuery was introduced to make development with JavaScript easier.

How do I inject JavaScript into WordPress?

The easiest way to add JavaScript to your WordPress footer is with a plugin like Insert Headers and Footers. This tool can insert code into the header and footer of your WordPress site without editing any theme files. To use Insert Headers and Footers, simply install and activate the plugin.


2 Answers

Firstly, you always have to use a no-conflict wrapper in Wordpress, so your code would look like :

jQuery(document).ready(function($){
    alert('Hello World!');
});

Secondly, it's good practice to put your javascript in external files, and in a Wordpress plugin you would include those like this :

wp_register_script( 'my_plugin_script', plugins_url('/my_plugin.js', __FILE__), array('jquery'));

wp_enqueue_script( 'my_plugin_script' );

This includes your script, and sets up jQuery as a dependency, so Wordpress will automatically load jQuery if it's not already loaded, making sure it's only loaded once, and that it's loaded before your plugins script.

And if you only need the script on the admin pages, you can load it conditionally using Wordpress add_action handlers:

add_action( 'admin_menu', 'my_admin_plugin' );

function my_admin_plugin() {
    wp_register_script( 'my_plugin_script', plugins_url('/my_plugin.js', __FILE__), array('jquery'));
    wp_enqueue_script( 'my_plugin_script' );

    // do admin stuff here
}
like image 101
adeneo Avatar answered Oct 17 '22 22:10

adeneo


It is not recommended to be using your own jquery version.

WordPress includes its own version of jquery and many other similar JS files, which have all been rigorously tested with WP and many of the most common plugins. In order to provide the best compatibility and experience for our users, we ask that you not package your own (especially not an older version) and instead use wp_enqueue_script() to pull in WordPress's version.

So you should use this instead:

wp_enqueue_script('jquery')
like image 23
pjehan Avatar answered Oct 17 '22 22:10

pjehan