2

What I want to achieve

I want to add a custom oembed provider to my wordpress site. The custom oembed provider is my own web app.

What I have implemented

I have added the following code to the functions.php file of my theme, following the official documentation and examples found online :

function custom_oembed_provider() {
  error_log("UPDATING PROVERS LIST FAILS HERE FOR SOME REASON");
  wp_oembed_add_provider('(https?:\/\/)?((www)\.)?myip:5000\/figure\/.*', 'http://myip:5000/services/oembed',true); 
}
add_action( 'init', 'custom_oembed_provider' );

The symptom I see

When I try to embed a content matching the URL pattern, I get the following error in return:

{
    "code": "oembed_invalid_url",
    "message": "Not Found",
    "data": {
        "status": 404
    }
}

The cause I have found

According the source code I have looked up, it comes from the fact that my url does not match any known provider.

And indeed, I checked thanks to the great wp-cli tool, and the list of know providers only contains the default wordpress whitelisted oembed providers described in the docs, as you can see on this screenshot:

result from list providers command

However, I know that the wp_omebed_add_provider has been called, it logs the "updating providers" message.

What I have tried

Some of the things I have tried with no success:

  • Using another custom provider than mine (it seems to have nothing to do with the provider itself)
  • Changing themes
  • Declaring in different ways and locations the wp_oembed_add_provider function
  • Updating php version
  • Running the same test on a local install of wordpress 5.7.2

Any idea how I could solve this? It is an important feature of the website I am building :( Thanks !

PS : I run PHP 7.3 and Wordpress 5.7.2 on a managed OVH Web public cloud.

  • Not sure, but ((www)\.)?152.228.212.94 can lead to an invalid url - like the error is saying. www.152.228.212.94 is not a valid hostname (the TLD needs to start with a-z letter, 94 does not conform to that) – hakre Jul 03 '21 at 13:17
  • Hi hakre, thanks for you reply. You are right that there is a glitch is the regex, however I cannot add any oembed provider, not only mine. So I think it is unrelated. For instance wp_oembed_add_provider('https://embedery.com/widget/*', 'https://embedery.com/api/oembed',false); leads to the same output (the provider is not added to the list). – Antoine Bacalu Jul 03 '21 at 13:36
  • Yes, it might not cause this, just something that sprung in my eye. Good to see this clarified! Is `http://152.228.212.94:5000/services/oembed` an oembed provider? Is it allowed in your WordPress configuration to have HTTP instead of HTTPS for them? And I'm not totally sure you need to have it in the database, but certainly it won't hurt. Just saying. – hakre Jul 03 '21 at 13:52
  • Yes it is an oembed provider, I tested it fine with another client than wordpress. I am not completely sure about http, but from my understanding of what wp_oembed_add_provider does, it shouldn't make a difference, since this function is only adding something to a list. I am not php fluent but what the function does seems straightforward in wordpress source code : https://developer.wordpress.org/reference/functions/wp_oembed_add_provider/#source – Antoine Bacalu Jul 03 '21 at 14:05
  • I can't test this right now, but I suspect your regex is being rejected. You'll need to wrap your regex string with regex delimiters. E.g. `#your_regex#i` also your regex is probably unnecessary anyway. You could capture it without regex and just use asterix for certain wildcards. You're also not escaping the periods in the IP address. – Paul G. Jul 03 '21 at 22:13
  • Hi Paul, you are absolutely right about the regex, thanks for spotting. I have switched back to a an Asterix to avoid confusion, but neither wp_oembed_add_provider(`'http://152.228.212.94:5000/figure/*'`, 'http://152.228.212.94:5000/services/oembed/',false); nor wp_oembed_add_provider('https://embedery.com/widget/*', 'https://embedery.com/api/oembed',false); seem to add a provider. Also I tried to remove existing providers by doing wp_oembed_remove_provider('#https?://(www\.)?tiktok\.com/.*/video/.*#i');, nothing happens to the list of providers either. (please note that li – Antoine Bacalu Jul 04 '21 at 12:14

1 Answers1

1

I have solved it, 3 things were overlapping hence the confusion:

  1. The list of custom providers printed by wp-cli seems not to print custom added providers, so it was confusing my debugging
  2. As some pointed out it the comments, my regex was not properly written (thanks)
  3. I had an extra / at the end of my test url, which was making the oembed provider crash, and the error returned by wordpress was 'oembed_invalid_url', I didn't see it