1 回答

TA貢獻1802條經驗 獲得超4個贊
使用該template_redirect
鉤子,您可以訪問將呈現到頁面的所有 HTML。您可以使用此掛鉤打開輸出緩沖,查找和替換您想要的任何內容,然后返回輸出,無論它是否已被修改。
請記住,這不會涵蓋通過延遲加載、AJAX 請求等動態加載的任何 iframe - 但在運行時加載到 HTML 中的任何內容都將在此處。
add_action( 'template_redirect', 'global_find_replace', 99 );
function global_find_replace(){
ob_start( function( $buffer ){
/**
*`$buffer` contains your entire markup for this page, at run time.
* anything dynamically loaded with JS/Ajax, etc won't be in here
*/
// Did they accept the GDPR cookie?
if( !isset($_COOKIE['gdpr_consent']) ){
// Nope. Build a simple "accept cookies" notice
$notice = '<div class="accept-cookies">You must accept cookies to see this content</div>';
// Replace all youtube iframes regardless of class, id, other attributes, with our notice
$buffer = preg_replace( '/<iframe.+src="https?:\/\/(?:www.)?youtu\.?be(?:\.com)?.+<\/iframe>/i', $notice, $buffer );
}
// Always return the buffer, wither it was modified or not.
return $buffer;
});
}
這是我為 youtube 視頻制作的正則表達式,如果我錯過任何內容,請隨時修改它:https ://regex101.com/r/2ZQOvk/2/
這應該足以讓你開始!
- 1 回答
- 0 關注
- 108 瀏覽
添加回答
舉報