2017-04-13 58 views
1

想阻止包含非https網址的oEmbed請求,但以下代碼無法幫助我。不知道這是否是正確的選擇。有任何想法嗎?如何屏蔽WordPress oEmbed HTTP網址

add_filter('pre_oembed_result', array($this, 'filter_oembed'), 5, 3); 

function filter_oembed ($result, $url, $args) { 
    if (substr($url, 0, 7) === "http://") { 
     return null; 
    } 
} 

我使用Wordpress 4.7.2。

回答

1

我沒有太多的Wordpress體驗,但the docs聽起來像是期待某種回報,並且只有當URL以「http://」開頭時纔會返回。此外,我認爲這是在某種類別中運行,否則在回調規範中使用$this將無法​​正常工作。順便說一句,PHP提供a built-in function分析一個URL:

<?php 
add_filter('pre_oembed_result', array($this, 'filter_oembed'), 5, 3); 

function filter_oembed ($result, $url, $args) { 
    if (parse_url($url, PHP_URL_SCHEME) !== "https") { 
     $result = false; 
    } 
    return $result; 
} 

Disable oEmbed for a Single Shortcode or at Least All Internal Links還可以揭示事物更多的光線。

+0

感謝鏈接你包括幫助..在你的答案應該返回false而不是返回null! – srvy

+0

好的,已更新。我不確定... – miken32