1

I have this code:

add_action('admin_init', 'wpse74389_check_username');
function wpse74389_check_username(){
$user = wp_get_current_user();
if( current_user_can('admin')){
    echo '<style>
li#menu-settings .wp-submenu.wp-submenu-wrap li:last-child {
display: none!important;
}
li#menu-settings .wp-submenu.wp-submenu-wrap li:nth-child(2) {
 display: none!important;
}
</style>';
}
}

I receive an error:

Cannot modify header information - headers already sent by

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
emiliano
  • 189
  • 2
  • 16

1 Answers1

2

You should use the admin_head action to add the code to WP-Admin.

For example, replace your code with this:

add_action('admin_head', 'wpse74389_check_username'); // <-- Modified

function wpse74389_check_username(){
    $user = wp_get_current_user();
    if( current_user_can('admin')){
        echo '<style>
        li#menu-settings .wp-submenu.wp-submenu-wrap li:last-child {
        display: none!important;
        }
        li#menu-settings .wp-submenu.wp-submenu-wrap li:nth-child(2) {
        display: none!important;
        }
        </style>';
    }
}

The admin_init fires too early in WordPress for you to directly echo code to screen. This is what causes the error message you're seeing.

Cannot modify header information - headers already sent by

This error often occurs if you attempt to print any text to screen (using echo or print or even having text before your opening <?php tag).

There's lots of information in this question, which focus on causes of the error.

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47