wordpress: add css file for your plugin admin page only

If you want to add a css file that is loaded only when your plugin admin page is displayed, here is how to do it:

Step 1: attach to appropriate hook:

add_action('admin_enqueue_scripts', 'myplugin_loadAdminCss');

Step 2: inside your hooked function, filter on provided parameter:

function myplugin_loadAdminCss($hook) {
        if (('plugins_page_XXX') != $hook) {
          return;
        }
        wp_register_style('myplugin_admin_css', plugins_url('/css/admin.css', __FILE__), false, '1.0.0);
}

Key here is to filter on value provided to your function (named $hook here).

This value corresponds to the admin page being currently displayed, with a special value when this page is the “plugins.php” page where $hook is ‘plugins_page_XXX’ where XXX = name used as 5th parameter when registering the plugin admin page via add_submenu_page()

sources

Post a Comment

Your email is never published nor shared. You're allow to say what you want...