I am looking for an efficient way to detect if user input is a valid embed link (and of what type). I know that one can cause WordPress to run its embed code with arbitrary input.

For example, like this:

$embed_code = wp_oembed_get($some_string);
    // do something clever

Assuming that there is at least some link, it will return a fully marked up link failing all else. The behaviour I am after, however, is to be able to feed it some user input (after appropriate sanitation) and get back either a fully marked-up embed or a FALSE but not a hyperlink if the content is a URL that it does not recognise. The purpose is to detect if the given input text is a valid embeddable URL.

Can (or should) I use wp_oembed_get to detect a valid embeddable link or is there a better way to do so?

1 Answer 1


The regex patterns for matching embeddable URLs are stored in WP_oEmbed, which has the method get_provider() for checking if a given URL is an embeddable URL for a supported provider. Just set the discover argument to false if you don't want to actually send a request to the URL to test it, and just want to match the regex:

$url      = 'https://twitter.com/WordPress/status/1169427892406644736';
$oembed   = new WP_oEmbed();
$provider = $oembed->get_provider( $url, [ 'discover' => false ] );

if ( false !== $provider ) {
    // Is embeddable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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