3

I need WP to show an embedded representation of a url such as http://domain.com/1121 using oembed.

I have setup http://domain.com/1121 with a discovery link:

<link href="http://www.domain.com/generator/oembed/?url=http%3A//domain.com/1121" rel="alternate" type="text/xml+oembed" />

I'm aware that WP no longer automatically discovers embeddable URLs, so I installed enable-oembed-discovery plugin to re-enable this feature.

I have monitored my apache logs and confirmed that WP does hits the link to discover - so I guess the enable-oembed-discovery plugin works, but the URL is NEVER converted.

I even wrote a plugin to whitelist my custom URL (really should not be needed since enable-oembed-discovery plugin should enable auto-discovery, but what the heck):

add_action( 'init', 'my_oembed_provider' );

function my_oembed_provider() {
    wp_oembed_add_provider( 'http://domain.com/*', 'http://www.domain.com/generator/oembed/', false);
}

and still nothing.

The API endpoint (http://www.domain.com/generator/oembed/) is hard coded to generate the following XML:

        <?xml version="1.0" encoding="UTF-8"?>
        <OEmbed>
            <type>rich</type>
            <width>400</width>
            <height>342</height>
            <html><![CDATA[&lt;b&gt;HELLO WORLD FROM OEMBED &lt;/b&gt;]]></html>
        </OEmbed>

The API endpoint sends the right content-type header (header("Content-type: text/xml");) as required by the oembed spec

The user adding the post has admin privileges and therefore is allowed unfiltered_html

I'm out of ideas and at wit's end. What am I missing?

2 Answers 2

3

Your XML root element was 'OEmbed', not 'oembed', as given in the spec. XML element names are case-sensitive, per the XML spec.

1
  • o_0 I missed that completely.. this is technically the right answer for my problem. Thank you.
    – Jahufar
    Jul 1, 2011 at 4:40
4

Two things:

  1. WordPress' oEmbed implementation prefers and works better with JSON.

  2. To use XML, WordPress needs the simplexml library in PHP to be available. If it's not available, then you'd probably get the results you're seeing. It's getting the XML but can't parse it without simplexml, so... nothing happens.

Best way to fix the problem: Stop trying to use XML. Use JSON instead.

The link type would be application/json+oembed.

The endpoint would return json data (using a content type of application/json):

{
"type":"rich",
"width":"400",
"height":"342",
"html":"&lt;b&gt;HELLO WORLD FROM OEMBED &lt;/b&gt;"
}

Things tend to work better that way. JSON is the preferred format for oEmbed with all providers.

2
  • 1
    Yeah, I was looking through the code (tig12.net/downloads/apidocs/wp3/wp-includes/…) a few mins back and the comments seem to indicate this. I'll change my endpoint to echo back JSON and see if it helps. Also: my config has simpleXml lib installed.. so XML parser should kick in
    – Jahufar
    Jun 30, 2011 at 11:26
  • I can't believe I spent all morning and most of the afternoon chasing after this. Implementing this in JSON as you said just solved it. Why on earth is there code that handles XML endpoints in WP's codebase when it doesn't obviously work?! Anyways, thanks a lot!
    – Jahufar
    Jun 30, 2011 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.