Register Custom WordPress oEmbed Providers with wp_oembed_add_provider and wp_embed_register_handler

Register custom oEmbed providers and embed handlers

Paste a YouTube or Vimeo link into a post and WordPress shows a player instead of the link. Behind it is oEmbed: WordPress asks the service’s oEmbed endpoint for the embed HTML of that URL. A list of trusted providers is built into core. Links from other services, an internal video platform for example, stay plain links, unless you register the service yourself.

Problem: Editors paste links from the company’s own video platform, and the posts show bare URLs instead of players.

Solution: If the platform has an oEmbed endpoint, add it to the providers with wp_oembed_add_provider(). If it has none, register an embed handler with wp_embed_register_handler() that builds the <iframe> from the URL.

A service with an oEmbed endpoint

<?php
add_action( 'init', function () {
    wp_oembed_add_provider(
        'https://videos.example.com/*',          // URLs that belong to the service
        'https://videos.example.com/oembed',     // its oEmbed endpoint
        false                                    // false = wildcard pattern, true = regular expression
    );
} );

WordPress sends ?url=...&format=json to the endpoint and inserts the html of the answer. Providers added this way are trusted: their HTML is used as it is.

Services that are not registered can still work through discovery: if the page has a <link rel="alternate" type="application/json+oembed"> tag, WordPress finds the endpoint itself. For such untrusted providers it accepts only an iframe and adds a sandbox attribute, which some players do not work with. Registering the provider avoids that.

A service without oEmbed

<?php
add_action( 'init', function () {
    wp_embed_register_handler(
        'ha_video',
        '#https?://player\.example\.com/v/([a-zA-Z0-9_-]+)#i',
        'ha_video_embed_handler'
    );
} );

function ha_video_embed_handler( $matches, $attr, $url, $rawattr ) {
    $width  = ! empty( $attr['width'] ) ? (int) $attr['width'] : 640;
    $height = (int) round( $width * 9 / 16 );

    $html = sprintf(
        '<iframe src="%s" width="%d" height="%d" frameborder="0" allowfullscreen loading="lazy"></iframe>',
        esc_url( 'https://player.example.com/embed/' . $matches[1] ),
        $width,
        $height
    );

    return apply_filters( 'embed_ha_video', $html, $matches, $attr, $url, $rawattr );
}

$matches are the groups of the regular expression; here $matches[1] is the video ID. The handler runs on the server, so no request to the service is made.

Removing a provider

To stop embedding one of the built-in services, remove it with the same pattern core uses, or filter the list:

<?php
add_filter( 'oembed_providers', function ( $providers ) {
    foreach ( array_keys( $providers ) as $pattern ) {
        if ( false !== strpos( $pattern, 'twitter\.com' ) ) {
            unset( $providers[ $pattern ] );
        }
    }
    return $providers;
} );

Cached results

The result of every embed is cached in post meta (_oembed_{hash} and _oembed_time_{hash}), also when it failed. After registering a new provider, links in existing posts may still show as plain links. Saving the post again refreshes its embeds; for many posts, delete the cached meta:

wp db query "DELETE FROM $(wp db prefix)postmeta WHERE meta_key LIKE '\_oembed\_%'"

The embeds are fetched again on the next view of each post, which is slower once per post.

NOTE: The HTML of a trusted provider is printed into your pages without filtering. Register only services you control or trust, and prefer an embed handler that builds the iframe from a known URL pattern when the service is your own.