I am learning WordPress plugin development. I am trying to attach a jQuery/JavaScript file in Admin Panel. I am using below code in plugins index.php
file.
function admin_script() {
wp_enqueue_script('my_custom_script', plugin_dir_url(__FILE__) . '/myscript.js');
}
add_action('admin_enqueue_scripts','admin_script');
Is it correct code ? What is my_custom_script
here?
I am learning WordPress plugin development. I am trying to attach a jQuery/JavaScript file in Admin Panel. I am using below code in plugins index.php
file.
function admin_script() {
wp_enqueue_script('my_custom_script', plugin_dir_url(__FILE__) . '/myscript.js');
}
add_action('admin_enqueue_scripts','admin_script');
Is it correct code ? What is my_custom_script
here?
1 Answer
Reset to default 1Yes, your code is good.
And that my_custom_script
is a unique identifier for the script you're enqueueing. You can find the default script handlers/identifiers here — e.g. jquery
for the jQuery library that ships with WordPress.
There you can also find more details about the wp_enqueue_script()
function, e.g. where should you use the function, the parameters, etc.