1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-12-02 14:16:11 +01:00
blog/content/posts/2008-12-11-how-to-set-a-custom-gravatar-image-in-wordpress-27.md

65 lines
3.2 KiB
Markdown
Raw Normal View History

2013-11-18 23:54:59 +01:00
---
2018-07-19 02:22:01 +02:00
type: post
2013-11-21 20:38:20 +01:00
2013-11-18 23:54:59 +01:00
title: 'HowTo: Set A Custom Gravatar Image In Wordpress 2.7+'
2013-11-21 20:38:20 +01:00
author: Matthias Kretschmann
date: 2008-12-11 22:59:06+00:00
2018-07-18 23:04:31 +02:00
2013-11-18 23:54:59 +01:00
tags:
2018-08-30 22:50:09 +02:00
- design
2015-06-07 23:13:18 +02:00
- tutorial
- wordpress
2016-04-17 21:07:03 +02:00
2017-11-25 17:24:23 +01:00
coinhive: true
2016-04-17 21:07:03 +02:00
redirect_from:
- /2008/12/how-to-set-a-custom-gravatar-image-in-wordpress-27/
2013-11-18 23:54:59 +01:00
---
2018-07-17 23:26:36 +02:00
![Wordpress Logo by kremalicious](../media/wordpress-logo.png)
2013-11-21 20:38:20 +01:00
Sure enough I've upgraded immediately when [Wordpress 2.7 was released](http://wordpress.org/development/2008/12/coltrane/). Among all the other things that changed in this new version the comments functions got a massive overhaul. But the [new comment loop](http://codex.wordpress.org/Migrating_Plugins_and_Themes_to_2.7/Enhanced_Comment_Display#The_Comments_Loop) with the [new function `<?php wp_list_comments(); ?>`](http://codex.wordpress.org/Template_Tags/wp_list_comments) lacks the ability to quickly set a custom default gravatar or avatar image. But with some help of the functions.php file we can set the default gravatar image in the Discussion settings in the Wordpress backend.
2013-11-18 23:54:59 +01:00
2018-08-08 22:26:42 +02:00
<!-- more -->
2013-11-18 23:54:59 +01:00
Before Wordpress 2.7 I achieved a custom gravatar image on kremalicious.com with this code placed in the comments.php template file:
2018-07-18 23:04:31 +02:00
```php
2015-06-07 23:00:52 +02:00
<?php
2018-09-29 02:54:31 +02:00
if(function_exists('get_avatar')) {
2015-06-07 23:00:52 +02:00
echo get_avatar(
2018-09-29 02:54:31 +02:00
$comment,
$size = '70',
$default = '<?php bloginfo('template_directory'); ?>/images/gravatar.png'
);
}
2014-09-10 20:13:28 +02:00
?>
2018-07-18 23:04:31 +02:00
```
2013-11-18 23:54:59 +01:00
2013-11-21 20:38:20 +01:00
So we were able to set a path to our image we wanted to use as the default gravatar image. But with Wordpress 2.7 we have the new function [`<?php wp_list_comments(); ?>`](http://codex.wordpress.org/Template_Tags/wp_list_comments) which pretty much simplifies writing comment template code. Although it has a parameter for the avatar size it doesn't have one for setting a custom image like before.
2013-11-18 23:54:59 +01:00
But we can use the functions.php file in your template directory and add some lines to it: (If you don't have a functions.php file just create one.)
2018-07-18 23:04:31 +02:00
```php
2017-11-25 17:24:23 +01:00
<?php
2018-09-29 02:54:31 +02:00
function my_own_gravatar( $avatar_defaults ) {
$myavatar = get_bloginfo('template_directory') . '/images/gravatar.png';
$avatar_defaults[$myavatar] = 'GRAVATAR NAME DISPLAYED IN WORDPRESS';
return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'my_own_gravatar' );
2014-09-10 20:13:28 +02:00
?>
2018-07-18 23:04:31 +02:00
```
2013-11-18 23:54:59 +01:00
Just set a name for your custom Gravatar image to show up beside the image in the Wordpress back-end. The code above assumes you have your custom default gravatar image inside a folder called images inside your template directory. Change it to your environment if neccessary. After that a new entry in the Wordpress backend under Settings > Discussions will appear with the custom image specified:
2018-07-17 23:26:36 +02:00
![custom gravatar](../media/custom-gravatar.jpg)
2013-11-18 23:54:59 +01:00
2013-11-21 20:38:20 +01:00
And you can adjust the displayed size of the gravatar image by adding a parameter to `<?php wp_list_comments(); ?>` function in your comments.php file:
2013-11-18 23:54:59 +01:00
2018-07-18 23:04:31 +02:00
```php
2013-11-21 20:38:20 +01:00
<?php wp_list_comments(array('avatar_size'=>70, )); ?>
2018-07-18 23:04:31 +02:00
```
2013-11-18 23:54:59 +01:00
2015-06-07 23:00:52 +02:00
And that's it. As you would guess I pretty much prefer this way to adjust the gravatar image. But you're free to [write your custom comment callback function](http://clarktech.no-ip.com/wordpress/wordpress-27-comment-callback-function) to exactly define the output of the comments. But it's definitely too much if you just want to change the gravatar stuff.