Sign up (with export icon)

Media embed configuration

Contribute to this guideShow the table of contents

You can configure the data output format and the list of supported media providers used by the MediaEmbed plugin.

Data output format

Copy link

You can configure the data output format of the feature using the config.mediaEmbed.previewsInData option.

Note

This option does not change how the media are displayed inside the editor. The previewable ones will still be displayed with previews. It only affects the output data (see below).

Semantic data output (default)

Copy link

By default, the media embed feature outputs semantic <oembed url="..."> tags for previewable and non-previewable media. That being so, it works best when the application processes (expands) the media on the server side or directly in the frontend, preserving the versatile database representation:

<figure class="media">
    <oembed url="https://media-url"></oembed>
</figure>
Copy code

You can achieve further customization of the semantic data output through the config.mediaEmbed.elementName configuration. As an example, if you set elementName to o-embed:

<figure class="media">
    <o-embed url="https://media-url"></o-embed>
</figure>
Copy code

If elementName is overridden to something other than the default value, the existing <oembed> elements will still be shown for backward compatibility purposes.

Including previews in data

Copy link

Optionally, by setting mediaEmbed.previewsInData to true you can configure the media embed feature to output media in the same way they look in the editor. If the media element is “previewable,” the media preview (HTML) is saved to the database:

<figure class="media">
    <div data-oembed-url="https://media-url">
        <iframe src="https://media-preview-url"></iframe>
    </div>
</figure>
Copy code

Currently, the preview is only available for content providers for which CKEditor 5 can predict the <iframe> code: YouTube, Vimeo, Dailymotion, Spotify, etc. For other providers like X (Twitter) or Instagram, the editor cannot produce an <iframe> code. It also does not allow retrieving this code from an external oEmbed service. Therefore, for non-previewable media, it produces the default semantic output:

<figure class="media">
    <oembed url="https://media-url"></oembed>
</figure>
Copy code

This means that, unless you limit the list of providers to only those that are previewable, you need to make sure that the media are displayed on your website.

Read more about non-previewable media.

Media providers

Copy link

CKEditor 5 comes with several supported media providers that you can extend or alter.

Names of providers with previews:

  • 'dailymotion',
  • 'spotify',
  • 'youtube',
  • 'vimeo'.

Names of providers without previews:

  • 'instagram',
  • 'twitter',
  • 'googleMaps',
  • 'flickr',
  • 'facebook'.
Note

The default media provider configuration does not support all possible media URLs – only the most common are included. Services like Iframely or Embedly support thousands of media providers. It is up to you to define which you want to allow.

Extending media providers

Copy link

To extend the default list of providers, use config.mediaEmbed.extraProviders.

Removing media providers

Copy link

To remove certain providers, use config.mediaEmbed.removeProviders.

For instance, to leave only the previewable providers, configure this feature as follows:

ClassicEditor
    .create( {
        // ... Other configuration options ...
        mediaEmbed: {
            removeProviders: [ 'instagram', 'twitter', 'googleMaps', 'flickr', 'facebook' ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );
Copy code

Overriding media providers

Copy link

To override the default providers, use config.mediaEmbed.providers and define your set according to the provider syntax:

ClassicEditor
    .create( {
        // ... Other configuration options ...
        mediaEmbed: {
            providers: [
                {
                    // A URL regular expression or an array of URL regular expressions:
                    url: /^example\.com\/media\/(\w+)/,

                    // To be defined only if the media are previewable:
                    html: match => '...'
                },
                // More providers.
                // ...
            ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );
Copy code

You can take inspiration from the default configuration of this feature. You can find it in https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-media-embed/src/mediaembedediting.ts