Best way to inject css into admin_head in plugins?

I’m looking for the best way to inject CSS into WordPress’ Admin CP.

Currently, I’m using the admin_head action hook and in this hook, I’m using dirname( __FILE__ ) to retrieve the directory for the stylesheets. However, dirname() does retrieve the server’s path. Is this the recommended way or is there some sort of WordPress function to get a URI path rather than a directory path?

    public function admin_head()
    {
        // Let's include the Control Panel CSS
        $url = dirname( __FILE__ ) . '/css/cpanel.css';
        $ie = dirname( __FILE__ ) . '/css/cpanel-ie.css';

        // Inject our cPanel styelsheet and add a conditionaly for Internet Explorer
        //      (fixes bugs on my home browser)
        $head = <<<HEAD
    <link rel="stylesheet" href="https://wordpress.stackexchange.com/questions/10477/{$url}" type="text/css" media="screen" />
    <!--[if IE]>
        <link rel="stylesheet" type="text/css" href="{$ie}" media="screen" />
    <![endif]-->
HEAD;

        echo $head; 

        foreach( self::$classes as $class => $obj )
        {
            if ( method_exists( $obj, 'admin_head' ) )
            {
                $obj->admin_head();
            }
        }
    }

-Zack

3 Answers
3

See plugins_url(). It’s perfect and engineered to link to files in plugin folders.

PS also wp_enqueue_style() might make sense in the mix.

Leave a Comment