Change logo url based on WP user role

I want to be able to change the WordPress default logo url (not the logo image) based on the user role. The image/logo will remain the same, only the url will change. Any assistance or ideas will be greatly appreciated.

Topic logo user-roles Wordpress

Category Web


try this filter for the logo url link; adapt your landing page links:

function user_defined_logo_url( $html ) {
$cur_user = wp_get_current_user();
$user_role = $cur_user->roles[0];
$logo_url = esc_url( home_url( '/' ) );
switch( $user_role ) {
case "cc1":
        $switch_logo_url = esc_url( home_url( '/' ) . 'landingcc1' );   
    break;
case "cc2":
        $switch_logo_url = esc_url( home_url( '/' ) . 'landingcc2' );   
    break;
case "cc3":
        $switch_logo_url = esc_url( home_url( '/' ) . 'landingcc3' );   
    break;
default:
        $switch_logo_url = '';  
}
if( $switch_logo_url ) $html = str_replace( 'href="'.$logo_url, 'href="'.$switch_logo_url, $html );
return $html;
}
add_filter( 'get_custom_logo', 'user_defined_logo_url' );`


It depends on your theme. Typically themes set the logo href dynamically to the home_url() function or site_url() function.

Any good theme will provide you a filter hook to amend this destination using add_filter, which would return a new url. e.g.

function change_logo_url ($url) {
  $url = "www.changeurlhere.com";
  return $url;
}

add_filter('your_themes_hook', 'change_logo_url');

About

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