Adding CSS styles to Admin Area PAGES only (not POSTS or CPT)

I am using this to load css styles in the admin area (for any user who is not an administrator):

function wpse_admin_styles() {
    if ( !current_user_can( 'manage_options' ) ) {
        echo 'style
                .fun-stuff-here {color:aqua;}
            /style
        ';
    }
}
add_action('admin_head', 'wpse_admin_styles');

But I need to have some styles only load if the admin page being viewed is a page (not a post or custom post type post)

Is this possible? I think the answer selected in this WPSE question has some potential, but I don't know how to add in the necessary code to what I have: How to add an admin function only to posts, not pages?

I also came across this Wordpress Support Forum post. The OP's reply near the bottom (8th post from the top) looked promising, but in use it applies the CSS to all Pages, Posts and Custom Post Type sections: http://wordpress.org/support/topic/add-new-page-button-in-admin-area

Topic conditional-tags css admin pages Wordpress

Category Web


I was having the same kind of problem, eventually I fixed it by calling the main css file of the front-end and making specific ID's for the admin-only stuffs like so:

function your_css() {
    wp_register_style('your_css', plugins_url('style.css',__FILE__ ));
    wp_enqueue_style('your_css');
}
add_action( 'init','your_css');

the wp_register_style & wp_enqeue_style are wordpress hooks/handlers to get the css file instead of placing the css code within your function ;)

Make sure to place it in the root plugin file!

I've got this follow up problem of 2 css files conflicting with each other so I'm still fiddling around with it, but at least your css file gets called and (partially) applied. Good Luck


Like the front-end in WordPress, the admin pages have various css classes added to the <body> tag.

You could use:

body.post_type-page .fun-stuff-here {
  color:aqua;
}

Keep your action the same - the CSS will be loaded everywhere on admin, but only becomes relevant in the Pages section.

(There's a post_type-... for each.)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.