有一点特殊的过滤方式WP_Embed
类,该类将独立链接转换为嵌入的目标:
/**
* 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 );
}
如果我们采用该逻辑并根据您的用例调整regex:
add_filter( \'the_content\', \'autoembed_list_items\', 8 );
function autoembed_list_items( $content ) {
global $wp_embed;
return preg_replace_callback( \'|<li>(https?://[^\\s"]+)</li>|im\', array( $wp_embed, \'autoembed_callback\' ), $content );
}