Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends


i want to add jquery code in my wordpress plugin so how i can do these ? i tried throw wp_enqeue_script in theme so it execute sucessfully but i want to do that for plugin so how i can do that ?
Posted

1 solution

Add the code to an external JavaScript file inside your plugin directory, then use wp_enqueue_script() to load it.

Here's how the call should be made:
PHP
<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ??>


Assuming you have your plugin file and js file in the same directory, your call will look something like this:
PHP
function load_my_plugins_javascript() {
    wp_enqueue_script( 'script-name', plugins_url( 'script.js' , __FILE__ ) , array( 'jquery' ), '1.0', true );
}

add_action( 'wp_enqueue_scripts', 'load_my_plugins_javascript' ); 


This adds your loader function to the array of other functions called by wp_enqueue_scripts(). By passing the parameter array( 'jquery' ) to $deps, you're telling WordPress to load the built-in jQuery script first, then load your custom script; this helps resolve dependency issues.

If you have your scripts in a subfolder inside your plugin, you'll want to use dirname( __FILE__ ) in the example above. Also, I'm assuming that your script is intended to run on the front-end of the site; if you are loading the script just for the admin side, you'll need to alter that call.

The Codex is the perfect source for this info. Check out http://codex.wordpress.org/Function_Reference/wp_enqueue_script[^] for the syntax and examples, and http://codex.wordpress.org/Function_Reference/plugins_url[^] for how to get the URL to a file inside your plugin.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900