oEmbedding twitter updates with Java and WordPress

December 20, 2011 by Michael

I’m really a big fan of oEmbed. My project Daily Fratze acts as oEmbed provider and consumer for example.

Now I’m really happy that twitter announced that it now acts as an oembed provider:

(I’d even be happier if twitter would autodiscover providers 😉 )

To use this in a Java based application you can use my java-oembed lib with the following configuration:

Oembed oembed = new OembedBuilder(this.httpClient)
			new OembedProviderBuilder()
	.withHandlers(new CommonHandler("twitter"))

The handler looks like this:

import org.apache.commons.lang.StringEscapeUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import ac.simons.oembed.OembedResponse;
import ac.simons.oembed.OembedResponseHandler;
public class CommonHandler implements OembedResponseHandler {
	private String handlerFor;
	public CommonHandler(String handlerFor) {
		this.handlerFor = handlerFor;
	public String getFor() {
		return handlerFor;
	public void handle(Document document, Element a, OembedResponse response) {
		final StringBuilder hlp = new StringBuilder();
		final String title = StringEscapeUtils.escapeHtml(response.getTitle());
		if(response.getType().equalsIgnoreCase("video") || response.getType().equalsIgnoreCase("rich")) {
			hlp.append("<span style=\"display:block; text-align:center;\">");
		} else if(response.getType().equalsIgnoreCase("photo")) {			
			hlp.append("<span style=\"display:block; text-align:center;\">");			
			hlp.append(String.format("<img src=\"%s\" alt=\"%s\" title=\"%s\" style=\"width: %d; height: %d;\" />", response.getUrl(), title, title, response.getWidth(), response.getHeight()));			

Be careful to get the latest release, twitter has some real large values for cache ages and i needed to update a member from int to long.

To have WordPress automatically embed statusupdates, add the following line to the “functions.php” of your current theme. Create the file if it isn’t available in the root folder of your theme:

wp_oembed_add_provider('#https?://twitter.com/\#!/[a-z0-9_]{1,20}/status/\d+#i', 'https://api.twitter.com/1/statuses/oembed.json', true);

Whenever you add the plain link (whithout an anchor tag) to a statusupdate on a single line in a post, it will be embedded like the example above.


If you are more into plugins, just download my Enable Twitter oEmbed WordPress plugin, install it and you’re good to go.

An alternative to oEmbed for Twitter was the Twitter Blackbird Pie Plugin for WordPress, but why adding more stuff if everything else is already there? My plugin is much more lightweight.

Embedding me looks like this, by the way:

michael | DailyFratze.de ...täglich frisch!

the picture will always show my latest update on daily fratze.

As this is probably the last post here for this year, i wish every visitor some nice christmas holidays!

No comments yet

Post a Comment

Your email is never published. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *