Fixing Twitter OEmbed Language in WordPress

A Twitter OEmbed returns tweets in the language of the requesting server, but say your an english site on a german server? You don’t want German retweet buttons! Here’s the fix.

What Needs Adding

The twitter OEmbed has some documented parameters, most notably, a lang attribute will fix the problem:

lang

Language code for the rendered embed. This will affect the text and localisation of the rendered HTML.

Example Valuesfr

https://dev.twitter.com/docs/api/1.1/get/statuses/oembed

So that lang attribute needs adding to the oembed request somehow. The most common method of doing this floating around on the net at the moment is a fix using new WordPress 3.5 filters.

The v3.5 fix

The fix that works for WordPress v3.5+ relies on a newly introduced filter called oembed_fetch_url.

View the code on Gist.

This works well, just define WP_LANG in wp-config.php and it’ll use that value.

A fix for 3.5 & 3.4

The above fix works well, however, at the time of writing v3.5 is currently at the release candidate stage, and may not be released for several weeks. A new fix is needed!

To do this, I’ve added a second fix to create one that works with v3.4 too:

View the code on Gist.

Here if the version is lower than 3.5, I replace the twitter OEmbed entry with one that has the necessary language attribute.

4 thoughts on “Fixing Twitter OEmbed Language in WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.