WordPress修改默认管理员avatar

Published
2023-01-12
浏览次数 :  161

function change_gravatar_to_local_image( $avatar, $id_or_email, $size, $default, $alt ) {
    $user = false;
    if ( is_numeric( $id_or_email ) ) {
        $id   = (int) $id_or_email;
        $user = get_user_by( 'id', $id );
    } elseif ( is_object( $id_or_email ) ) {
        if ( ! empty( $id_or_email->user_id ) ) {
            $id   = (int) $id_or_email->user_id;
            $user = get_user_by( 'id', $id );
        }
    } else {
        $user = get_user_by( 'email', $id_or_email );
    }
    if ( $user && is_object( $user ) ) {
        $local_image = get_template_directory_uri() . '/images/avatar.jpeg';
        $avatar = preg_replace( "/src='(.*?)'/i", "src='{$local_image}'", $avatar );
        $avatar = preg_replace( "/srcset='(.*?)'/i", "srcset='{$local_image}'", $avatar );
        update_user_meta($user->ID, 'local_avatar', $local_image);
    }
    return $avatar;
}
add_filter( 'get_avatar', 'change_gravatar_to_local_image', 10, 5 );

  • 标签1
  • 标签1
  • 标签1
Top