. */ new kl_addnewdefaultavatar( ); class kl_addnewdefaultavatar { function kl_addnewdefaultavatar( ) { add_filter( 'avatar_defaults' , array( &$this , 'addavatar' ) ); add_filter( 'admin_init' , array( &$this , 'register_fields' ) ); add_filter( 'get_avatar', array( &$this, 'insert_img_correctly' ), 10, 5 ); } function register_fields() { register_setting( 'discussion', 'kl_addnewdefaultavatar', array( &$this, 'validate') ); add_settings_field('anda_setting', __('Add New Default Avatar' , 'anda' ) , array(&$this, 'anda_fields') , 'discussion', 'avatars', $args = array()); } function anda_fields() { $value = get_option( 'kl_addnewdefaultavatar', array( 'name' => 'Custom Avatar', 'url' => 'http://colorto.me/png/%size%/row2/purple/yellow/orange/blue/' ) ); echo ''; echo ''; echo '

' . __( 'Some themes won\'t resize your image to fit, so it\'s best to use an image that\'s already the right size.', 'anda' ); echo '
'; echo sprintf( __( 'However if your image accepts a size argument, you can use %1$s. Example: %2$s', 'anda' ), '%size%', 'http://colorto.me/png/%size%/row2/purple/yellow/orange/blue/' ) . '

'; } function validate( $input ) { $input[ 'name' ] = esc_attr( $input[ 'name' ] ); $input[ 'url' ] = esc_url( $input[ 'url' ] ); return $input; } function addavatar ( $avatar_defaults ) { if ( $opts = get_option( 'kl_addnewdefaultavatar' ) ) { $av = html_entity_decode( $opts['url'] ); $avatar_defaults[ $av ] = $opts['name']; } return $avatar_defaults; } function insert_img_correctly( $avatar, $id_or_email, $size, $default, $alt ) { //determine email address if ( is_numeric($id_or_email) ) $email = get_userdata( $id_or_email )->user_email; elseif ( is_object($id_or_email) ) $email = $id_or_email->comment_author_email; else $email = $id_or_email; //since we're hooking directly into get_avatar, //we need to make sure another avatar hasn't been selected $direct = get_option('avatar_default'); if ( strpos( $default, $direct ) !== false ) { $email = empty( $email ) ? 'nobody' : md5( $email ); //in rare cases were there is no email associated with the comment (like Mr WordPress) //we have to work around a bit to insert the custom avatar // 'www' version for WP2.9 and older if ( strpos( $default, 'http://0.gravatar.com/avatar/') === 0 || strpos( $default, 'http://www.gravatar.com/avatar/') === 0 ) $avatar = str_replace( $default, $direct, $avatar ); } //hack the correct size parameter back in, if necessary $avatar = str_replace( '%size%', $size, $avatar ); $avatar = str_replace( urlencode('%size%'), $size, $avatar ); return $avatar; } }