Skip to content

Comment avatar functions ignore $args['default'] #2468

@tylersticka

Description

@tylersticka

Expected behavior

If you want to customize the default avatar in WordPress from a plugin or theme, normally you'd do so via one of the avatar filters like so:

function example_custom_avatar_data($args, $id_or_email) {
  $args['default'] = 'https://placeimg.com/96/96/animals';
  return $args;
}

add_filter('pre_get_avatar_data', 'example_custom_avatar_data', 10, 2);

In which case any request for a Gravatar that does not exist will instead display our custom default image:

Screen Shot 2021-07-14 at 9 32 55 AM

This functionality is helpful in cases where we want to extend WordPress's default avatar functionality dynamically via a plugin.

Actual behavior

In Timber, the following code in the context of a post comment…

<img src="{{comment.avatar}}" width="96" height="96" alt="">

…will never display the image path set via the filter using $args['default']. It will always fall back to get_option('avatar_default') unless $default is provided from the template.

Screen Shot 2021-07-14 at 9 37 12 AM

Steps to reproduce behavior

  1. Activate the Timber starter theme.
  2. Add the Twig snippet above to the theme's templates/comment.twig.
  3. Add the PHP snippet above to the theme's functions.php or to a new plugin.
  4. Navigate to wp-admin/options-discussion.php and scroll to the "Default avatar" section. All default avatars should display the image path provided in the PHP snippet.
  5. Navigate to a post containing comments with a default image visible. Observe that the image displayed does not match what was defined in the filter.

What version of WordPress, PHP and Timber are you using?

  • WordPress 5.7.2
  • PHP 7.4.1
  • Timber 1.18.2

How did you install Timber? (for example, from GitHub, Composer/Packagist, WP.org?)

  • WP.org, synced locally via Local

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions