Fixing Broken Social Twitter Avatars

If you’ve been using Social for a while like I have, you might also be seeing broken images in the comments area of your old posts. I did a little digging into this and discovered that this is related to how Twitter handles user avatars.

When Twitter returns a user object, they include the URL of the user’s avatar. But that URL isn’t a reliable URL (like Facebook provides). Instead it’s a URL to a file location on a CDN. Over time these URLs change, likely relating to users changing their avatars.

Social grabs the provided user avatar URL when creating comments from Twitter replies, retweets, or for comments by people who log in with Twitter and faithfully sticks it in for appropriate comments – even when that URL has stopped working.

Since Twitter doesn’t provide a reliable URL that we can use over time, I ended up creating a little plugin that asks Twitter for the current user avatar for each Twitter account related to Social comment activity and updates the comments with the new image URLs accordingly.

This plugin runs a one-time action, asking Twitter for user information 100 users at a time and updating your comments at that time. So you’ll need to run it every so often to keep things looking and working properly. Since it is a fairly intensive action, I didn’t think putting it on a CRON was a good idea.

When you activate the plugin you’ll see a “Run” link in it’s section on the Plugins page. Click that and it will hit Twitter and do its thing.

Social Twitter Avatar Fix

It’s worked well for my site but that’s about the extent if the testing that’s been done with it. Normal caveats like “make sure you have backups” apply.

That’s it – I hope you’ll find this useful if you’re using Social with your WordPress site.

Download, fork and contribute on GitHub.

This post is part of the project: Social. View the project timeline for more context on this post.