add_action('admin_menu', 'myplugin_add_pages'); //hook into the admin menu action to add my page
function myplugin_add_pages() { // Add a submenu under Tools
$page = add_submenu_page( 'tools.php', 'My Plugin', 'My Plugin', 'activate_plugins', 'my-plugin', 'myplugin_page'); // store the result as a variable so we can associate our scripts with this page
add_action( "admin_print_scripts-$page", 'myplugin_admin_scripts' ); // hook in here to print scripts on our unique page
}
function myplugin_admin_scripts(){ // queue up the scripts
wp_enqueue_script('myscript', content_url() . '/plugins/myplugin/scripts/myscript.js', array('jquery')); //this one depends on jquery which will be loaded immediately before it.
}
What do you mean by “limit my javascript file to only my plugin” ?
I assume you include it only when your plugin is executed, so if you have any functions in it, you should be ok if you give them unique names? Something like pluginname_functionname instead of just functionname which might be similar to a function already existing?
When my plugin is activated with the code I wrote (above), it is included in EVERY admin page.
I am just thinking it’s unnecessary for it to load in every admin page since it’s only needed when someone is interacting with the plugin it’s made for.