問題描述
我想在我的 WordPress 主題中使用 HTML5,如何關閉 wptexturize?
我不介意 WP 新增中斷,但我希望他們是<br> 而不是<br /> 。如何控制我的程式碼中出現的這些錯誤?
編輯:我只是非常關心<br> 標籤問題,我不介意排版的變化。
EDIT2:其實,我猜<img> 標籤也是重要的。任何 self-closing 獨立標籤都將重要。所以<hr> 也可能是一個問題。更不用說像<link> 這樣的 wp_head()產品和各種<meta> 標籤。
最佳解決方案
換行符由 wpautop()而不是 wptexturize()新增。 wpautop()也是自動新增段落標籤的功能。
您更好地修復<br />,而不是更換過濾器。由於 wpautop()的優先順序為 10,所以您可以在此之後掛鉤並修復它。
add_filter( 'the_content', 'html5_line_breaks', 25 );
function html5_line_breaks( $content ) {
return str_replace( '<br />', '<br>', $content );
}
OP 更新後編輯:
WordPress 功能旨在輸出 XHTML 。為了擺脫那些尾隨的斜槓 site-wide,你將不得不使用輸出緩衝區。您可以使用與上述相似的過濾器替換帖子內容中的斜槓,但是不會抓住您的頭部,側邊欄等。
這有點醜陋,可能會對效能有一個小的影響,但是在這裡你可以 (放在一個外掛或你的主題的 functions.php 檔案中):
if ( !is_admin() && ( ! defined('DOING_AJAX') || ( defined('DOING_AJAX') && ! DOING_AJAX ) ) ) {
ob_start( 'html5_slash_fixer' );
add_action( 'shutdown', 'html5_slash_fixer_flush' );
}
function html5_slash_fixer( $buffer ) {
return str_replace( ' />', '>', $buffer );
}
function html5_slash_fixer_flush() {
ob_end_flush();
}
該程式碼說如果您不在管理區域,而不是執行 AJAX 請求處理,然後透過過濾器開始緩衝輸出,然後使用 WordPress 關閉鉤子,輸出該緩衝區。
次佳解決方案
幹得好:
function my_awesome_tag_fixer( $input ){
return preg_replace( '/(<.+)s/>/', '$1>', $input );
}
foreach( array('the_content', 'the_excerpt', 'comment_text') as $filter )
add_filter( $filter, 'my_awesome_tag_fixer', 12 );
這不是最優雅的解決方案,但它比重寫 wpautop 和 wptexturize 更快地完成它。
第三種解決方案
這可以在例如該主題的 function.php 檔案利用 remove_filter()功能 (http://codex.wordpress.org/Function_Reference/remove_filter)
remove_filter("the_content", "wptexturize");
第四種方案
剛找到它 void 元素上的 self-closing 標籤是有效的 html 。
In HTML5 we've allowed the / on void elements (like <meta>, <img>, <br>, <input>, etc), to ease migration to and from XML.
http://lists.whatwg.org/pipermail/help-whatwg.org/2008-August/000137.html
更多資訊:
http://wiki.whatwg.org/wiki/FAQ#Should_I_close_empty_elements_with_.2F.3E_or_.3E.3F
第五種方案
我有一個 html5 和 WordPress 的起始主題,也是一個功能不是為了 wptexturize,而是 for wpautop() 。還包括 html 的其他元素,像 thead,tfoot,旁邊,並使用 html5 的語法和
/**
* wpautop for HTML5, allowed: table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|header|footer|hgroup|figure|details|figcaption|summary)
* @link http://nicolasgallagher.com/using-html5-elements-in-wordpress-post-content/
* @author nicolas@nicolasgallagher.com
*/
function html5wpautop($pee, $br = 1) {
if ( trim($pee) === '' )
return '';
$pee = $pee . "n"; // just to make things a little easier, pad the end
$pee = preg_replace('|<br />s*<br />|', "nn", $pee);
// Space things out a little
// *insertion* of section|article|aside|header|footer|hgroup|figure|details|figcaption|summary
$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|header|footer|hgroup|figure|details|figcaption|summary)';
$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "n$1", $pee);
$pee = preg_replace('!(</' . $allblocks . '>)!', "$1nn", $pee);
$pee = str_replace(array("rn", "r"), "n", $pee); // cross-platform newlines
if ( strpos($pee, '<object') !== false ) {
$pee = preg_replace('|s*<param([^>]*)>s*|', "<param$1>", $pee); // no pee inside object/embed
$pee = preg_replace('|s*</embed>s*|', '</embed>', $pee);
}
$pee = preg_replace("/nn+/", "nn", $pee); // take care of duplicates
// make paragraphs, including one at the end
$pees = preg_split('/ns*n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
$pee = '';
foreach ( $pees as $tinkle )
$pee .= '<p>' . trim($tinkle, "n") . "</p>n";
$pee = preg_replace('|<p>s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
// *insertion* of section|article|aside
$pee = preg_replace('!<p>([^<]+)</(div|address|form|section|article|aside)>!', "<p>$1</p></$2>", $pee);
$pee = preg_replace('!<p>s*(</?' . $allblocks . '[^>]*>)s*</p>!', "$1", $pee); // don't pee all over a tag
$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
$pee = preg_replace('!<p>s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)s*</p>!', "$1", $pee);
if ($br) {
$pee = preg_replace_callback('/<(script|style).*?</\1>/s', create_function('$matches', 'return str_replace("n", "<WPPreserveNewline />", $matches[0]);'), $pee);
$pee = preg_replace('|(?<!<br />)s*n|', "<br />n", $pee); // optionally make line breaks
$pee = str_replace('<WPPreserveNewline />', "n", $pee);
}
$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)s*<br />!', "$1", $pee);
// *insertion* of img|figcaption|summary
$pee = preg_replace('!<br />(s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol|img|figcaption|summary)[^>]*>)!', '$1', $pee);
if (strpos($pee, '<pre') !== false)
$pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee );
$pee = preg_replace( "|n</p>$|", '</p>', $pee );
return $pee;
}
// remove the original wpautop function
remove_filter('the_excerpt', 'wpautop');
remove_filter('the_content', 'wpautop');
// add our new html5autop function
add_filter('the_excerpt', 'html5wpautop');
add_filter('the_content', 'html5wpautop');
看看更多的 svn 的 html5 開始主題,不是一個框架!
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。