Skip to content

Fix: Custom avatar not shown if set default avatar - #411

Merged
polevaultweb merged 3 commits into
developfrom
#403-Custom-avatar-not-shown-if-set-default-avatar
May 30, 2026
Merged

Fix: Custom avatar not shown if set default avatar#411
polevaultweb merged 3 commits into
developfrom
#403-Custom-avatar-not-shown-if-set-default-avatar

Conversation

@reygcalantaol

@reygcalantaol reygcalantaol commented Aug 16, 2025

Copy link
Copy Markdown
Collaborator

Resolves #403

Summary

Changes the get_avatar_url filter priority for custom avatars from 10 to 11 so that a user's uploaded avatar takes precedence over the site-wide default avatar set in WPUM settings.

Root cause: Both set_avatar_url (custom avatar) and set_default_avatar (default avatar) were registered at priority 10. WordPress executes same-priority filters in registration order, meaning the default avatar could overwrite the custom one.

Fix: Bump set_avatar_url to priority 11 so it always runs after set_default_avatar.


Manual Test Plan

Prerequisites

  • WP User Manager active
  • Two test user accounts (one admin, one subscriber)

1. Setup default avatar

  • Go to Users > Settings > Profiles
  • Enable Custom Avatars
  • Upload a default avatar image (e.g. a grey silhouette)
  • Save settings
  • Expected: The default avatar appears for users who haven't uploaded a custom avatar

2. Upload a custom avatar for a user

  • Go to Users > Edit for the subscriber user
  • In the Avatar section, upload a different image (e.g. a coloured photo)
  • Save the user
  • Expected: The custom avatar saves without error

3. Verify custom avatar overrides default

  • Visit the subscriber's frontend profile page (/profile/{username}/)
  • Expected: The custom uploaded avatar is shown, NOT the default avatar
  • Visit a user directory page if one exists
  • Expected: The subscriber shows their custom avatar, other users show the default

4. Verify default avatar still works for other users

  • Visit the admin user's profile (who has NOT uploaded a custom avatar)
  • Expected: The default avatar is shown (not a Gravatar or blank)

5. Verify Gravatar fallback

  • Remove the custom avatar from the subscriber (clear the field, save)
  • Visit their profile page
  • Expected: Falls back to the default avatar (not a broken image)

WPUnit Test Coverage

4 tests in tests/wpunit/Avatars/AvatarPriorityTest.php:

  • set_avatar_url registered at priority 11
  • set_default_avatar registered at priority 10
  • Custom avatar priority is higher than default avatar priority
  • WPUM_Avatars class exists

🤖 Generated with Claude Code

polevaultweb and others added 2 commits February 18, 2026 12:28
Verify that the set_avatar_url filter runs at priority 11 (after the
set_default_avatar filter at priority 10), ensuring custom avatars
take precedence over the site-wide default avatar.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an execution-order bug in WPUM’s avatar URL filtering so that a user’s uploaded (custom) avatar reliably overrides the site-wide default avatar when both are configured.

Changes:

  • Change WPUM_Avatars::set_avatar_url filter registration priority from 10 to 11 so it runs after set_default_avatar.
  • Add WPUnit coverage asserting the expected get_avatar_url filter priorities and their ordering.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
includes/admin/class-wpum-avatars.php Bumps custom avatar filter priority to ensure it wins over the default avatar filter.
tests/wpunit/Avatars/AvatarPriorityTest.php Adds WPUnit tests to confirm filter priorities/order for custom vs default avatar behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +32 to +36
public function _tearDown() {
// Remove filters that our fresh instance added, so they don't leak
// into other tests.
remove_all_filters( 'get_avatar_url' );

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove_all_filters( 'get_avatar_url' ) removes every callback on that hook (including core/other plugin/test callbacks), which can cause cross-test pollution. Prefer removing only the WPUM_Avatars callbacks you added (e.g., iterate current callbacks and remove_filter where the callable object is a WPUM_Avatars instance), or snapshot/restore the prior callbacks in _tearDown().

Copilot uses AI. Check for mistakes.
Comment on lines +3 to +9
* Tests for the WPUM_Avatars filter priority fix (#411).
*
* When both a custom avatar and a site-wide default avatar are configured,
* the custom avatar filter (set_avatar_url) must run AFTER the default avatar
* filter (set_default_avatar) so the custom avatar takes precedence.
*
* @see https://github.com/WPUserManager/wp-user-manager/pull/411

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test header hard-codes PR references (#411 / pull/411). This is brittle and can become incorrect when cherry-picked or backported; prefer referencing the tracked issue (e.g., #403) or describing the behavior without linking to a specific PR number.

Suggested change
* Tests for the WPUM_Avatars filter priority fix (#411).
*
* When both a custom avatar and a site-wide default avatar are configured,
* the custom avatar filter (set_avatar_url) must run AFTER the default avatar
* filter (set_default_avatar) so the custom avatar takes precedence.
*
* @see https://github.com/WPUserManager/wp-user-manager/pull/411
* Tests for the WPUM_Avatars filter priority behavior.
*
* When both a custom avatar and a site-wide default avatar are configured,
* the custom avatar filter (set_avatar_url) must run AFTER the default avatar
* filter (set_default_avatar) so the custom avatar takes precedence.

Copilot uses AI. Check for mistakes.
Comment on lines +92 to +113
/**
* Helper: return the WPUM_Avatars instance that was created in _setUp.
*
* We retrieve it from the filter registry rather than storing a reference,
* so the test truly reflects what WordPress sees.
*
* @return WPUM_Avatars
*/
private function get_avatars_instance() {
global $wp_filter;

if ( ! isset( $wp_filter['get_avatar_url'] ) ) {
$this->fail( 'get_avatar_url filter is not registered.' );
}

foreach ( $wp_filter['get_avatar_url']->callbacks as $priority => $callbacks ) {
foreach ( $callbacks as $callback ) {
if (
is_array( $callback['function'] ) &&
$callback['function'][0] instanceof WPUM_Avatars
) {
return $callback['function'][0];

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_avatars_instance() reaches into the $wp_filter internals (WP_Hook::$callbacks) and returns the first WPUM_Avatars instance it finds. This is brittle across WP versions and also doesn’t guarantee it returns the instance created in _setUp() if another WPUM_Avatars instance is already registered. Consider storing the instance created in _setUp() on the test class (and using has_filter on that), or making the helper explicitly select the most recently added callback.

Copilot uses AI. Check for mistakes.
@polevaultweb
polevaultweb merged commit 7bbda35 into develop May 30, 2026
13 checks passed
@polevaultweb
polevaultweb deleted the #403-Custom-avatar-not-shown-if-set-default-avatar branch May 30, 2026 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom avatar not shown if set default avatar

3 participants