. */ 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( ) { // the function this filter runs in is pluggable // may not have all the arguments you'd expect list( $avatar, $id_or_email, $size, $default, $alt ) = func_get_args(); //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; //get selected option $selected = get_option('avatar_default'); //get custom options (for comparison) $extras = apply_filters( 'avatar_defaults', array() ); //for matching, below //remove size query added to custom options $default_minus_size = str_replace("?s=$size", '', $default ); //only do this processing if: //the 'selected' avatar is one of our cutsom options (most cases. selected is global setting.) //or //the 'default' avatar is one of our custom options (admin list. default passed to func) if ( isset( $extras[ $default_minus_size ] ) || isset( $extras[ $selected ] ) ) { $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, $selected, $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; } }