0

Im currently in the process of writing a plugin that allows me to import content from a different Content Management System into WordPress.

Almost everything works as I want it to: Each entry gets saved with title, content and featured image. I even managed to transfer and update images within the content area. But now im struggling with one last problem.

If theres an embedded video (youtube/vimeo) in the content, and I save this as an iframe, it doesn't behave the same way as the wordpress oEmbeds.

Normally when you paste a url to a video in the WordPress editor, it generates the embed code for you, while only saving the url to the database. Im not really sure how this works, but even if I strip out the iframe from the html, and add only the url, it still requires me to go to the post, and press update, in order for the iframe to work in the front end.

Another thing i have realised, is that WordPress don't actually save

tags to the database, but if I insert the data from this other CMS, all the html tags gets inserted with it (of course). If I then go to the post and press update - all the data gets formatted nicely.

Is there a function that i can use to "prepare" the raw html coming from this other WYSISYG, before saving it to the wordpress database? Or is there a way to save the embeds, in order to make them work?

I feel like WordPress core must be using a function when saving from the editor to the database - but I can't seem to find anything about such a function.

Any clarification on this subject is appreciated.

2

Wrap the url in the [embed] shortcode. This will force wordpress to call it's oEmbed implementation.

    [embed]https://www.youtube.com/watch?v=YE7VzlLtp-4[/embed]

Or, if you want to be able to control the height/width

   [embed height="400" width="600"]https://www.youtube.com/watch?v=YE7VzlLtp-4[/embed]

Note, this will only work for websites that are wordpress's whitelist of sites. You can add sites to this list using wp_embed_register_handler

Edit
If you want to check if your provider is in the whitelist, could consider doing something like this (Note, I've never tested the below before):

    function prefix_is_whitelisted_embed($url){
        require_once( ABSPATH . WPINC . '/class-oembed.php' );

        if ( did_action( 'plugins_loaded' ) ) { //if you do this before plugins are loaded you may prevent other plugins from adding urls to the whitelist
            $oEmbed = _wp_oembed_get_object();
            return $oEmbed->get_provider($url) === false?false:true;
        }
    }

This function basically checks to see if a url has a registered provider using the WP oEmbed object. If it does, the class method get_provider() returns the provider, if it doesn't it should return bool false. This function just checks for that bool false and returns based on that.

  • Thank you - this did the trick. Bonus question: Do you know if theres a way to test if URL is whitelisted. Something like: wp_is_whitelisted_embed($url). – Malibur Aug 10 '15 at 13:05
  • I think you could do this with _wp_oemebed_get_object(), see my updated response above. – TheGentleman Aug 10 '15 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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