0

So I'm trying to add a new default avatar for my theme using the following code:

add_filter( 'avatar_defaults', 'mytheme_default_avatar' );

function mytheme_default_avatar ( $avatar_defaults ) {
    $new_avatar_url = get_template_directory_uri() . '/images/default_avatar.png';
    $avatar_defaults[$new_avatar_url] = 'Default Avatar';
    return $avatar_defaults;
}

After I do that, the new avatar appears on the administration, but it's not checked by default. Is there any way I can manipulate that admin option via functions.php? It's essential, since I have different color skins in my theme and I use different avatar images for each of them.. So once the user change the color skin, another default avatar will appear and the option will change itself again (since the old image will be gone).

So basically I'm looking for a way to change the "Default Avatar" option under "Settings -> Discussion" via functions.php in a natural way. Hope that makes sense.

Thanks in advance!

2

You can just update the option field avatar_default to your new image url:

add_filter( 'avatar_defaults', 'mytheme_default_avatar' );
function mytheme_default_avatar( $avatar_defaults ) 
{
    $avatar = get_option('avatar_default');

    $new_avatar_url = get_template_directory_uri() . '/images/default_avatar.png';

    if( $avatar != $new_avatar_url )
    {
        update_option( 'avatar_default', $new_avatar_url );
    }

    $avatar_defaults[ $new_avatar_url ] = 'Default Avatar';
    return $avatar_defaults;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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