Network-wide default avatar

Is it possible to set a network-wide default avatar? Sites should then be able to adjust it if they'd like (or using a plugin that does this), but until they set their own site-specific avatar I'd prefer they have my network avatar in lieu of the ridiculous mystery man.

  • kathy_darling

    Hi Joe,

    No offense intended! :slight_smile:

    That plugin is almost there but it doesn't set the new avatar as the default on all new sites. I really like the forked version for allowing you to upload new avatars, but ultimately it isn't working any more since it uses TimThumb to resize the avatars and Gravatar has changed their API to not allow that any more.

    I did solve it though

    add_filter( 'avatar_defaults', 'kia_default_avatar' );
    add_action( 'wpmu_new_blog', 'kia_new_blog_default_avatar' );
    
    // add to list of default avatars
    function kia_default_avatar ($avatar_defaults) {
    	$ib_avatar[$url] = 'Network Avatar';
    	return array_merge( $ib_avatar,$avatar_defaults ); // Put our custom avatar on top
    }
    
    // set a new blog to have the default avatar
    function kia_new_blog_default_avatar( $blog_id ){
    	switch_to_blog($blog_id);
    	update_option( 'avatar_default', esc_url( $url ) );
    	restore_current_blog();
    }

    Where $url should be replaced with the URL to the desired image. Now each newly created site will have this as the default until they change it or add their own.

    Thanks!

  • kathy_darling

    Joe,

    Based on some feedback from Otto on WPSE I have modifed my code to use only filters. I just learned about the default_option_$option and pre_option_$option filters. But knowing the action that fires when a new site is created will probably be handy in the future.

    I added this as its own php file in the mu-plugins folder.

    Cheers,
    -Kathy

    <?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();