admin管理员组

文章数量:1414908

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);
if($embed_code!=FALSE){
    // 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?

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);
if($embed_code!=FALSE){
    // 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?

Share Improve this question asked Sep 12, 2019 at 7:37 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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/WordPress/status/1169427892406644736';
$oembed   = new WP_oEmbed();
$provider = $oembed->get_provider( $url, [ 'discover' => false ] );

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

本文标签: embedCan I use wpoembedget to detect a valid embeddable link or is there a better way to do so