Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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();
share|improve this answer

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.

share|improve this answer
    
Thanks Otto. My problem with this is that it precludes the individual site owners from ever being able to change or create their own defaults. –  helgatheviking Nov 14 '12 at 20:30
    
Okay, in that case, you just use the default_option_avatar_default hook instead. Then the code will apply for the "default", which is the case where they have not created their own avatar setting yet. –  Otto Nov 14 '12 at 20:32
    
Well neat. Learned something new about the default_option_$option filter. Works great on the front end, but doesn't appear in the Discussion settings and can never be reverted to. I kind of like my solution for not confusing the individual site admins. –  helgatheviking Nov 14 '12 at 21:01
    
Well, if you want to add to the options then there's other filters you can use too. Filters are better than overriding the entire function, no matter how you slice it. –  Otto Nov 15 '12 at 1:42
    
I didn't think I was overriding an entire function, just setting an option on new blog creation. Would you take a look at my updated solution? I am using the default_option_avatar_default in conjunction with avatar_defaults filter. Is this what you had in mind? –  helgatheviking Nov 15 '12 at 17:30

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; 
    }
    
share|improve this answer
    
+1 A real improvement would be to serve it from the media library and kill this slow HTTP request over to gravatar.com –  kaiser Nov 14 '12 at 17:18
    
get_avatar filter will allow you to do that... –  Oleg Butuzov Nov 14 '12 at 17:21
    
From the question, I think this is exactly what was asked "(...) I'd prefer they have my avatar than the mystery man or some of the others (...)" –  kaiser Nov 14 '12 at 17:36
    
method same, usign filter "get_avatar" to change picture, all code to mu-plugin. –  Oleg Butuzov Nov 14 '12 at 17:42
    
thanks @CodeMonkey re: pre_option_ will the users be able to select their own option later? and re: filtering get_avatar... how does one filter this to only effect our default? very annoying that WP puts a gravatar request in front of a local URL! –  helgatheviking Nov 14 '12 at 17:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.