Home > database >  Can't add a php code to functions.php file
Can't add a php code to functions.php file

Time:04-21

I want to hide certain elements of the wpadminbar if you're not an admin but are logged in.

<?php if(is_user_logged_in() && !current_user_can(administrator)){  

        #wp-admin-bar-wp-logo{display: none;}

         #wp-admin-bar-site-name{display: none;}
}
?>

I tried adding this code to the last line of functions.php file. The error I get is:

"Your PHP code changes were rolled back due to an error on line 533(which is the first line of the code) of file wp-content/themes/vtube/functions.php. Please fix and try saving again.

syntax error, unexpected '<', expecting end of file"

CodePudding user response:

If recommend you hide the entire thing for non-admins and your own component to show the things you do want them to see.

Code for hiding the admin bar for non-admins (I use this code myself, but seem to remember I've borrowed it from an answer on Stackoverflow so don't credit me for this):

function prefix_hide_admin_bar( $show ) {
    if ( ! current_user_can( "administrator" ) ) {
      return false;
    }

    return $show;
  }
add_filter( "show_admin_bar", "prefix_hide_admin_bar" );

CodePudding user response:

First off, you shouldn't be modifying functions.php. A WP update could overwrite your changes.

You should put this in a plugin and when your if() condition is met, include a CSS file with the changes you want. There's documentation on including styles in the WordPress codex and you might have to tweak your CSS to get what you want.

  • Related