Using WordPress ’embed_oembed_discover’ PHP filter

The embed_oembed_discover WordPress PHP filter enables or disables the inspection of a given URL for discoverable <link> tags.

Usage

add_filter('embed_oembed_discover', 'your_custom_function');
function your_custom_function($enable) {
    // your custom code here
    return $enable;
}

Parameters

  • $enable (bool): Whether to enable <link> tag discovery. Default is true.

More information

See WordPress Developer Resources: embed_oembed_discover

Examples

Disable oEmbed Discovery

Disable oEmbed discovery for all URLs.

add_filter('embed_oembed_discover', '__return_false');

Enable oEmbed Discovery for Specific Domain

Enable oEmbed discovery only for URLs from a specific domain (e.g., example.com).

add_filter('embed_oembed_discover', 'enable_oembed_for_example_domain', 10, 1);
function enable_oembed_for_example_domain($enable) {
    $url = wp_extract_urls(get_the_content());
    if (strpos($url[0], 'example.com') !== false) {
        return true;
    }
    return false;
}

Disable oEmbed Discovery for YouTube

Disable oEmbed discovery for YouTube URLs.

add_filter('embed_oembed_discover', 'disable_oembed_for_youtube', 10, 1);
function disable_oembed_for_youtube($enable) {
    $url = wp_extract_urls(get_the_content());
    if (strpos($url[0], 'youtube.com') !== false) {
        return false;
    }
    return $enable;
}

Enable oEmbed Discovery Based on User Role

Enable oEmbed discovery only for posts by authors with the ‘editor’ role.

add_filter('embed_oembed_discover', 'enable_oembed_for_editors', 10, 1);
function enable_oembed_for_editors($enable) {
    $user = wp_get_current_user();
    if (in_array('editor', $user->roles)) {
        return true;
    }
    return false;
}

Enable oEmbed Discovery for Custom Post Type

Enable oEmbed discovery only for a custom post type called ‘portfolio’.

add_filter('embed_oembed_discover', 'enable_oembed_for_portfolio', 10, 1);
function enable_oembed_for_portfolio($enable) {
    if (get_post_type() == 'portfolio') {
        return true;
    }
    return $enable;
}