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.
-
the logo code has a filter at the end to filter the generated $html; developer.wordpress.org/reference/functions/get_custom_logo - what roles are you trying to check, and whereto are you trying to link them?– MichaelFeb 29, 2020 at 4:13
-
I'm trying to check for 3 custom roles (cc1, cc2, cc3) and change the default WP logo url to their respective landing pages (specific url's).– JimmyFeb 29, 2020 at 16:09
3 Answers
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');
Check out a few tips for your different options here
https://developer.wordpress.org/reference/functions/current_user_can/
If the current user is an administrator or editor
// example
if (current_user_can( 'edit_posts' )) {
// serve a URL for this kind of user
}
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' );`