So, I am new to PHP, so I’m hoping to do this with a plugin, if possible.
We are hosting on Heroku, this means uploading a local image to /uploads isn’t going to work for this. We need to be able to reference the individual user’s avatar from our hosting at Amazon s3 (they don’t need to be able to upload it themselves, we have a limited number of authors). We need a way to set the avatar to look to our s3 URL.
Is there a plugin that asks for a URL for an avatar instead of a local file?
Thank you.
The function that does all the heavy lifting for avatars is
get_avatar
. It’s a pluggable function, meaning you can replace it with a plugin. Get avatar also has a handy filter you can use to override things programatically. So, a few options: (1) completely replaceget_avatar
which might have unintended side effects and break things like avatars in comments, (2) Role your own function just for author avatars, (3) Filterget_avatar
for authors and leave it alone for everyone else.Either way, it’s starts with adding a field to user profiles so you can specify an avatar to do that.
Let’s start by wrapping everything in a class.
You’ll need to hook into
show_user_profile
andedit_user_profile
. The first shows fields on your own profile (and authors will be able to see the field as well) andedit_user_profile
will show the field when you’re editing other users.This also adds a little helper function to fetch the user avatar url. Pretty self explanatory: stick in a nonce, add a header, and echo out the field.
Adding the field doesn’t actually take care of saving it, however. You need to hook into
edit_user_profile_update
(fires when saving profiles other than your own) andperson_options_update
(fires when you save your own profile). In the hooked function we check our nonce, check to make sure the current user can edit, and save the data withupdate_user_meta
or delete it withdelete_user_meta
.You could use the above in your template directly (somewhere in the loop):
Which is kind of messy. Or we can try filtering the avatar with
get_avatar
:Theoretically the above should leave normal avatars alone, and use your custom avatars when they’re available. It probably needs to be tested more than I was able to.
Here is all that, wrapped in a plugin.
Chipps!
You can try filtering the
get_avatar
function. Let’s try shall we?Step 1
Place the above into your functions.php file.
How this works
First our function
custom_avatar
meta_key
namedcustom_avatar_url
the result of which is stored in our variable$avatar
FALSE
we proceed to check for the existence of a gravatar which is what WordPress will do by defaultFALSE
then we fallback to a default image placeholder held within our WordPress installation. (you can alter this to something entirely custom as well)If you don’t want to check for a Gravatar you don’t have to! Just remove that conditional check altogether.
Step 2
The above function handles the retrieval of the custom image URL you specify, but you need somewhere to specify that URL too.
Indicates that we are storing the custom image URL as
user_meta
, much likepost_meta
, users can also have meta information attached to them for all kinds of purposes, such as this one.We need to add a form input field on the User Profile page, so place this into your functions.php file as well;
Result
If you’ve not been copying and pasting as you read along then,
The entire script as a whole (paste into your functions.php file)
Notes
The variables,
$size
$default
$alt
…are all optional. So if you decide to remove any of those from the function arguements defined in,
Such as,
Then you need to also remove
$arg3, $arg4, $arg5
from the body of the function itself and also change the required arguments of the filter from,to