Add JS to admin panel
-
Hi, I’m sure this is just a simple matter of knowing what the action is, but I’ve so far not found it.
Have a plugin. Need to create options page. Options page is added via
function sexycalAdminMenu() { add_options_page('Birthday Calendar Administration', 'sexyBdayCal', 'manage_options', 'sexyBdayCal', 'sexycalAdminOptions'); } add_action('admin_menu', 'sexycalAdminMenu');
That part works beautifully, but I want to add some JS which I obviously only want loaded when the options-general.php?page=sexyBdayCal is active.
I’m having trouble figuring out which add_action I need to use to accomplish that. I’d prefer to add it in the head and not the footer, just because I’m a purist snob.
Thanks.
-
The action is ‘admin_enqueue_scripts’. Your callback must call
wp_enqueue_script()
to cause the JS page to load. (It loads in head by default but you can specify footer if you wanted to) The callback is passed a reference to the admin page being loaded so you can conditionally enqueue your script. You should probably experimentally var_dump or error_log that reference to be sure of what string to check for in your final callback version.This worked –
function sexycalAdminEnqueueJS($hook) { if ((strcmp($hook, 'settings_page_sexyBdayCal')) == 0) { wp_enqueue_script('jquery'); wp_enqueue_script('updateSexyAdmin', (WP_PLUGIN_URL . '/sexyBdayCalendar/js/updateSexyAdmin.js'), array('jquery'), false, false); } } add_action('admin_enqueue_scripts', 'sexycalAdminEnqueueJS');
To figure out the hook, I had to first
var_dump($hook)
but I can see why that is the hook after I saw it.
- The topic ‘Add JS to admin panel’ is closed to new replies.