默認(rèn)情況下,WordPress 評論表單允許用戶在評論中包含基本的HTML標(biāo)簽,這包括<p>、 <br>、 <strong > 等等。在大多數(shù)情況下,這些基本標(biāo)簽足以添加基本格式。但對于某些網(wǎng)站,可能需要更多標(biāo)簽,才能實(shí)現(xiàn)需要的功能。
例如,在一個服務(wù)器配置分享網(wǎng)站上,用戶需要通過評論分享 Apache 的 .htaccess 代碼。但由于 WordPress 允許的 HTML 標(biāo)簽有限,當(dāng)用戶試圖分享包含 Apache 標(biāo)記(如<IfModule>、 <Directory > 或<VirtualHost> 等 )的評論時,WordPress 評論過濾系統(tǒng)就會刪除這些標(biāo)記。任何不允許的標(biāo)記(無論是 HTML 標(biāo)記還是其他標(biāo)記)這會享受到這個待遇。
為了去掉這個限制,WordPress 提供了一種簡單的方法來自定義允許的評論標(biāo)記,在上面的情況中,我們可以添加以下函數(shù)來允許用戶在評論中添加需要的標(biāo)簽:
function wprs_add_allowed_tags() {
global $allowedtags;
$allowedtags['p'] = array('class' => true, 'id' => true);
$allowedtags['ul'] = array('class' => true, 'id' => true);
$allowedtags['ol'] = array('class' => true, 'id' => true);
$allowedtags['li'] = array('class' => true, 'id' => true);
$allowedtags['pre'] = array('class' => true, 'id' => true);
$allowedtags['a'] = array('class' => true, 'id' => true, 'title' => true, 'data-url' => true, 'data-date' => true, 'data-title' => true, 'href' => true);
$allowedtags['span'] = array('class' => true, 'id' => true, 'title' => true, 'data-url' => true, 'data-date' => true, 'data-title' => true);
$allowedtags['strong'] = array('class' => true, 'id' => true, 'title' => true, 'data-url' => true, 'data-date' => true, 'data-title' => true);
$allowedtags['ifmodule'] = array('class' => true, 'mod_rewrite.c' => true, 'mod_alias.c' => true, 'mod_auth.c' => true);
$allowedtags['directory'] = array();
$allowedtags['virtualhost'] = array();
$allowedtags['info'] = array();
$allowedtags['note'] = array();
$allowedtags['update'] = array();
}
add_action('init', 'wprs_add_allowed_tags', 11);
除了默認(rèn)允許的基本 HTML 標(biāo)記外,這段代碼還添加了前面提到的 Apache 標(biāo)記,以及一些內(nèi)部用于保存記錄等的一些標(biāo)記。
如果您需要類似的功能,可以直接把上面代碼添加到主題或插件中,然后根據(jù)自己的需要調(diào)整其中允許的標(biāo)記即可。


