好的,明白了。我只是在wp core中翻了一下,找到了他们用来抓取自动检测的功能。WP使用phppreg_replace_callback WP内的功能autoembed 作用下面是代码,如中所示wp-includes/class-wp-embed.php:
/**
     * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding.
     *
     * @uses WP_Embed::autoembed_callback()
     *
     * @param string $content The content to be searched.
     * @return string Potentially modified $content.
     */
    function autoembed( $content ) {
        return preg_replace_callback( \'|^\\s*(https?://[^\\s"]+)\\s*$|im\', array( $this, \'autoembed_callback\' ), $content );
    }
 因为我只打算让客户在每篇文章中输入一个视频,所以我使用
preg_match 与此相同的RegEx模式一起使用,以实现我想要的:
function embed_url_lookup() {
    if (!have_posts()) return false;
    the_post(); // necessary to use get_the_content()
    $reg = preg_match(\'|^\\s*(https?://[^\\s"]+)\\s*$|im\', get_the_content(), $matches);
    if (!$reg) return false;
    return trim($matches[0]);
} // end embed_url_looku
 这将找到第一个自动嵌入url并返回它。