亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在接受 cookie 同意之前替換所有 youtube iframe

在接受 cookie 同意之前替換所有 youtube iframe

PHP
aluckdog 2022-07-29 10:08:13
這是針對 GDPR cookie 政策的。因為 youtube 使用 cookie,所以我必須阻止 youtube 視頻,并且只有在接受 cookie 政策時才授予訪問權限。所以我需要類似的東西:if(!isset($_COOKIE['consentaccept'])) {    // replace all iframes from https://www.youtube.com/ with:    //<div class="youtubeblock">you must enable cookies to view this video</div>}或者你有更好的解決方案有任何想法嗎?它是WordPress。
查看完整描述

1 回答

?
慕虎7371278

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/

這應該足以讓你開始!



查看完整回答
反對 回復 2022-07-29
  • 1 回答
  • 0 關注
  • 108 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號