Site logo

Archived topic

How to add custom CSS to admin area and for certain user ID only

3 replies · Started by Alberto on May 13, 2021

Viewing posts 1–4 of 4

Hello there,

I would need a hint in order to add some CSS to admin area, only for certain user id (2).

For now, I have set up a hook element, with custom hook name set to admin_head

I later found this code:

add_action('admin_enqueue_scripts', 'custom_function_name');function custom_function_name() {
 global $current_user;
 $user_id = get_current_user_id();
 if(is_admin() && $user_id == '2'){
    wp_enqueue_style( 'admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' );
}}

I do not know PHP well, how could I adapt it to type inline CSS directly in the PHP function?

Thank you in advance for your help,
Alberto

Hi there,

i assume you're using an element to add some inline <style> - to do the same in a function you would do:

add_action('admin_head', function(){
    echo '<style>
    /* your CSS rules here */
    </style>';
});

Hi David,

thank you so much for your help!

I managed to apply that through functions.php

add_action('admin_head', 'custom_function_name');function custom_function_name() {
 global $current_user;
 $user_id = get_current_user_id();
 if(is_admin() && $user_id == '2'){
        echo '<style>
    #wpadminbar {
	display:none !important;
	}
	#adminmenumain {
	display:none !important;
	}
    </style>';
}}

Through GP Elements I probably need to change hooks settings to make it work... If I set hook custom name as "admin_head" the CSS is then applied to other admin users as well. I will leave it in functions.php :)

Alberto

Just a note you can include template tag conditions in the Hooks code itself - but as you're writing that in your code, it makes sense to keep it in functions.php.

Glad to hear its working!

This archived topic is closed to new replies.