skip to Main Content

I’m using Flatsome theme.

I want to change the logo based on the user role of the current user.

In the flatsome-child folder I’ve accessed the functions.php.

Just trying to change the logo isn’t working, so I’ve tried with some code, giving me nada change.

I’ve googled on how to change custom logo, but all I find is how you can change it in WordPress, but I need to have two different logos based on user role.

<?
add_filter('get_custom_logo', 'helpwp_custom_logo_output', 10);
function helpwp_custom_logo_output() {
    $html = '<a href="https://www.linkhere.com/" title="linkhere.com" rel="home">';
    $html .= '<img width="278" height="73" src="https://www.linkhere.com/wp-content/uploads/2019/11/linkhere-logo-custom.png" class="header_logo header-logo" alt="linkhere" scale="0">';
    $html .= '<img width="278" height="73" src="https://www.linkhere.com/wp-content/uploads/2019/11/linkhere-logo-custom.png" class="header-logo-dark" alt="linkhere" scale="0">';
    $html .= '</a>';

    $html = str_replace('header_logo', 'logo', $html );
    return $html;
}
?>

2

Answers


  1. WordPress stores user roles as capabilities in the user meta table. You can format your page templates like so:

    <?php if (current_user_can('administrator')) : ?>
    <img src="admin_logo.png" />
    <?php elseif (current_user_can('editor')) : ?>
    <img src="editor_logo.png" />
    <?php else : ?>
    <img src="logo.png" />
    <?php endif; ?>
    

    Give custom path of your logo image above

    Login or Signup to reply.
  2. WordPress has a function called

    current_user_can( string $capability )
    

    https://developer.wordpress.org/reference/functions/current_user_can/

    It returns TRUE or FALSE.

    The capabilities are:
    ‘delete_user’, ‘edit_user’, ‘remove_user’, ‘promote_user’, ‘delete_post’, ‘delete_page’, ‘edit_post’, ‘edit_page’, ‘read_post’, or ‘read_page’

    User roles are supported in part, like ‘administrator’ or ‘editor’.

    With this function you can do something like this:

    if(current_user_can( 'read_post' )) {
        //show a header image
    } else if(current_user_can( 'edit_post' )) {
        //show a header image
    } else {
        //show standard header
    }
    

    Regards Tom

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search