Set default avatar network-wide

I’ve seen plugins and snippets on how to set a new default avatar for a site. I’m wondering if this can be done network-wide, such that every new site on the network will have the network’s avatar as its primary default. from there the plugin’s that allow the user to upload a new avatar will be sufficient to let each site define it’s own. But until they do I’d prefer they have my avatar than the mystery man or some of the others which are downright ugly.

Related posts

Leave a Reply

3 comments

  1. The avatar_default is the better option.

    add_filter('pre_option_avatar_default', 'se72578_avatar_default');
    function se72578_avatar_default( $option ){
       return 'http://example.com/your/default/here.jpg';
    }
    

    Simple. Stick that in a mu-plugin and you’re good to go.

    Edit: If you want to still allow site owners to change the default avatar, use the default_option_avatar_default hook instead.

  2. Must Use Plugins will anable functionality global. And actualy functionality is pretty simple… only prefiltring default_avatar value.

    Use this code as plugin which you can install to anable default avatar.

    <?php
        /*
        Plugin Name: Network Default Avatar
        Description:  This Plugin Used to be the General Framework
        Author: 
        Version: 
        Author URI:
        */
    
        add_filter('pre_option_avatar_default', 'pre_option_avatar_default');
        function pre_option_avatar_default(){
            return 'mystery';
        }
    

    Update New code

    1. Using get_avatar filter hook you can replace the original avatar

      add_filter('get_avatar', 'get_avatar_custom',10,5);
      function get_avatar_custom($avatar, $id_or_email, $size, $default, $alt){
         // your code here...
            /*
            ** Example code will check if current avatar string is contain "mystery" avatar if yes, it will 
            ** try to get site_default_avatar property from options table (you can create a UI for entering this value)
            ** if system_default_avatar exists - filter will repalce default value (first argument of this filter callback)
            ** and return new value
            ** if (strpos($avatar, '561e2e7a9910204ba43fa4097e45c5e9') !== false 
            **    && get_option('site_default_avatar', false) != ''){
            **    $avatar =  "<img alt='".esc_attr($alt)."' src='".get_option('site_default_avatar', false)."' 
            **        class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
            ** } 
            **
            */
          return $avatar;
      }
      
    2. Using a… get_avatar function. get avatar is plugable function, which mean you can ovveride it in any plugin, theme or in our case in mu-plugin

      function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false {
         // your code here
         return $some_html_with_avatar_picture; 
      }
      
  3. In my mu-plugins folder I now have the following code, which adds a custom avatar as the default network wide, but allows each individual site to change it thereafter.

    <?php
      /*
        Plugin Name: Network Default Avatar
        Description:  This Plugin sets the default avatar network-wide
        */
    
        class Network_Default_Avatars {
    
        // configure these two variables
        static $name = "Network Default";  // text description of avatar... ie "Mystery Man"
        static $avatar = "http://example.com/default-avatar.png";  // URL of avatar
    
        function __construct() {
    
            // default avatar
            add_filter( 'avatar_defaults', array( $this, 'default_avatar' ) );  
            add_action( 'wpmu_new_blog', array( $this, 'new_blog_default_avatar' ) );
    
        }
        // add to list of default avatars
        function default_avatar ($avatar_defaults) {  
            $ib_avatar[self::$avatar] = self::$name;  
            return array_merge( $ib_avatar,$avatar_defaults ); // Put our custom avatar on top
        }
    
        // set a new blog to have the default avatar
        function new_blog_default_avatar( $blog_id ){
            switch_to_blog($blog_id);
            update_option( 'avatar_default', esc_url( self::$avatar ) );
            restore_current_blog();
        }
    
    }
    
    new Network_Default_Avatars();
    

    EDIT #1
    Per @Otto’s comment about the default_option_avatar_default_filter I’ve changed the new blog update_option action to use the default filter instead.

    <?php
      /*
        Plugin Name: Network Default Avatar
        Description:  This Plugin sets the default avatar network-wide
        */
    
        class Network_Default_Avatars {
    
        // configure these two variables
        static $name = "Network Default";  // text description of avatar... ie "Mystery Man"
        static $avatar = "http://example.com/default-avatar.png";  // URL of avatar
    
        function __construct() {
    
            // default avatar
            add_filter( 'avatar_defaults', array( $this, 'avatar_defaults' ) );  
            add_filter( 'default_option_avatar_default', array( $this, 'default_avatar' ) ); 
    
        }
    
        // add to list of default avatars
        function avatar_defaults ($avatar_defaults) {  
            $ib_avatar[self::$avatar] = self::$name;  
            return array_merge( $ib_avatar,$avatar_defaults ); // Put our custom avatars on top
        }
    
    
        function default_avatar () {  
            return self::$avatar; 
        }
    
    }
    
    new Network_Default_Avatars();