Search code examples
phpwordpressoembedwp-cli

Custom oembed provider not getting added to Wordpress


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.


Solution

  • 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