admin管理员组

文章数量:1122832

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.

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.

Share Improve this question asked Feb 29, 2020 at 2:16 JimmyJimmy 11 bronze badge 2
  • 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? – Michael Commented Feb 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). – Jimmy Commented Feb 29, 2020 at 16:09
Add a comment  | 

3 Answers 3

Reset to default 0

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

https://stackoverflow.com/questions/13404284/wordpress-capabilities-and-current-user-can-in-functions-php#13404440

// 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' );`

本文标签: Change logo url based on WP user role