Apply dashboard avatar as default avatar – LearnDash Dashboard

This document provides a code snippet to integrate the ld_dashboard_get_avatar_data function from the Ld_Dashboard_Public class with the pre_get_avatar_data filter in WordPress.

Code Snippet

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

function custom_ld_dashboard_avatar( $args, $id_or_email ) {
    // Load the class instance (adjust the way you access it as per your implementation).
    if ( ! class_exists( 'Ld_Dashboard_Public' ) ) {
        return $args; // Exit if the class is not loaded.
    }

    // Assuming the class instance is accessible via a singleton pattern.
    $ld_dashboard_instance = Ld_Dashboard_Public::instance();

    if ( method_exists( $ld_dashboard_instance, 'ld_dashboard_get_avatar_data' ) ) {
        // Use the custom avatar function.
        $custom_avatar_args = $ld_dashboard_instance->ld_dashboard_get_avatar_data( $args, $id_or_email );
        return $custom_avatar_args;
    }

    // Fallback to default args if the method doesn't exist.
    return $args;
}

Explanation

Hooking into pre_get_avatar_data

This filter allows overriding the default avatar data before it is processed by WordPress.

Checking for Class and Method

Ensure the Ld_Dashboard_Public class and the ld_dashboard_get_avatar_data method are loaded and available.

Delegating to ld_dashboard_get_avatar_data

If the method exists, call it with the necessary parameters and return its result.

Fallback to Default

If the class or method is not available, return the original $args.

Implementation Notes

  • Adjust Ld_Dashboard_Public::instance() to match how your class instance is accessed.
  • Replace this code in a suitable location, such as your theme’s functions.php file or a custom plugin.
  • Make sure Ld_Dashboard_Public is initialized before this filter is executed.

Expected Behavior

This snippet will ensure that avatars for all users are determined by the ld_dashboard_get_avatar_data function. Custom avatars, Gravatars, or the default LearnDash avatar will be used based on the logic defined in the ld_dashboard_get_avatar_data method.

Last updated: April 9, 2025