This how I do that lately, it is no exactly CSS specific but related because like with CSS you have similar problems. I abstract everything into it's own admin_page class so the admin page function hooks are properly set up when instantiating the class. This is for the page load hook for example which get's registered directly after the page was added into the menu:
The page load hook (in which you can enqueue scripts and CSS for example) is properly registered and will be only called when applicable. So inside the hook function you do not need to care about if it's the right situation to do something. It always is. So that's nice when you make modifications.PHP Code://Add the submenu page to "settings" menu
$label = __($this->_fullName, $this->_identifier);
$hookname = add_submenu_page($this->_pluginParent, $label, $this->_shortName, $access_level, $this->_identifier, array($this, 'admin_page'));
// register load hook
add_action('load-'.$hookname, array($this, 'admin_page_load'));
// register contextual help
add_action('contextual_help', array($this, 'contextual_help'), 10, 2);
It's not so nice for the contextual_help hook. This one is not reflecting a certain page so a check need to be done inside the hook. You can immediately see that this is making things more complicated, so the actual contextual help provider is another function:
This time the decision is based on some value that get's passed as a hook's parameter. Since this admin page is class based, things like the identifier are shared between all members (and therefore hooks). This makes it much more easy to deal with changes in the underlying hooks or APIs.PHP Code:/**
* contextual_help action hook function
*
* @param string $contextual_help
* @param string $screen
* @return string
*/
function contextual_help($contextual_help, $screen)
{
// add contextual help on current screen, keep compatibility with 2.8, 2.9 and 3.0
is_object($screen) && ((isset($screen->base) && $screen = $screen->base) || $screen='');
if('settings_page_' . $this->_identifier === $screen)
{
$contextual_help = $this->_get_contextual_help();
$this->_hasContextualHelp = true;
}
return $contextual_help;
}
Please note that this is an example. When you register the contextual_help action hook inside admin_page_load, the check should not be needed any longer.


LinkBack URL
About LinkBacks
Reply With Quote