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?
1 Answer
Reset to default 3The 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
版权声明:本文标题:embed - Can I use wp_oembed_get to detect a valid embeddable link or is there a better way to do so? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745165210a2645638.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论